找不到xml片段

时间:2013-09-16 06:37:17

标签: xml xml-parsing include xml-formatting xmlinclude

我正在尝试将xml片段文件包含在xml文件中并从java代码访问parent.xml。

Java代码如下:

DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
dfactory.setXIncludeAware(true);
dfactory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
DocumentBuilder builder = dfactory.newDocumentBuilder();
Document doc = builder.parse(new FileInputStream("C:/Users/admin/Desktop/parent.xml"));

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter writer = new StringWriter();

transformer.transform(new DOMSource(doc), new StreamResult(writer));

String xmlFile;
xmlFile = writer.toString();
System.out.println(xmlFile);

parent.xml文件如下所示:

<xi:include href="child.fragment" xmlns:xi="http://www.w3.org/2001/XInclude">
</xi:include>

parent.xml和child.fragment位于同一位置C:/Users/admin/Desktop/ 但是java代码找不到child.fragment ..

我收到了以下错误:

[Warning] Include operation failed, reverting to fallback. Resource error reading file as XML (href='child.fragment'). Reason: C:\Users\admin\Desktop\child.fragment (The system cannot find the file specified)
[Fatal Error] An include with href 'child.fragment'failed, and no fallback element was found.
Exception in thread "main" org.xml.sax.SAXParseException; An include with href 'child.fragment'failed, and no fallback element was found.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at MainClass.main(MainClass.java:30)

1 个答案:

答案 0 :(得分:0)

可能是找不到child.xml的程序。 href属性,包含指向要包含的文件的URL。如果使用后备,您可以获得有关此问题的更好信息。

<xi:include href="child.fragment" xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:fallback>
    <para>
      <emphasis>FIXME: MISSING XINCLUDE CONTENT</emphasis>
    </para>
  </xi:fallback>
</xi:include>

如果使用包含有许多限制。请检查this link

相关问题