简体   繁体   English

找不到xml片段

[英]Can't find xml fragment

I am trying to include xml fragment file in xml file and accessing parent.xml from java code. 我试图将xml片段文件包含在xml文件中,并从java代码访问parent.xml。

Java code look like: 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);

The parent.xml file looks like: parent.xml文件如下所示:

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

parent.xml and child.fragment are at the same location C:/Users/admin/Desktop/ But the java code can not find child.fragment.. parent.xml和child.fragment位于同一位置C:/Users/admin/Desktop/但是Java代码找不到child.fragment。

I got following error: 我收到以下错误:

[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)

May be the program not found a child.xml. 可能是程序找不到child.xml。 The href attribute that contains a URL pointing to the file to include. href属性包含指向要包含的文件的URL。 If use fallback you are get better information of this problem. 如果使用回退,则可以更好地了解此问题。

<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>

If use include have a number of limitations. 如果使用包括有许多限制。 Please check this link . 请检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM