0

I am trying to include xml fragment file in xml file and accessing parent.xml from java code.

Java code look like:

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:

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

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)
Kaustubh Najan
  • 879
  • 1
  • 7
  • 8

1 Answers1

0

May be the program not found a child.xml. The href attribute that contains a URL pointing to the file to include. 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.

herry
  • 1,708
  • 3
  • 17
  • 30