I have an XML file that has the following:
<contexts>
<context name="a">
<xi:include xmlns:xi="http://www.w3.org/2003/XInclude" href="reuse.xml"/>
<other_stuff/>
</context>
<context name="b">
<xi:include xmlns:xi="http://www.w3.org/2003/XInclude" href="reuse.xml"/>
<other_stuff/>
</context>
</contexts>
... and the reuse.xml referenced by the XInclude above which contains something like this:
<good_stuff/><!-- and a bunch of complex stuff ->
I'm trying to write some Java code which can generate some string output for each context element (easy) but instead of the xi:include line, replace that with the contents of the included file i.e. give me an 'exploded' String for each Context like this:
<context name="a">
<good_stuff/><!-- and a bunch of complex stuff ->
<other_stuff/>
</context>
Rather than what I'm getting now:
<context name="a">
<xi:include xmlns:xi="http://www.w3.org/2003/XInclude" href="reuse.xml"/>
<other_stuff/>
</context>
Hopefully I've explained myself clearly that I don't want the xi:include text in my output, but rather the contents of the other file it should be replaced by.
I'm setting the parser to be XInclude and Namespace aware:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setXIncludeAware(true);
...
Then getting the child nodes of the type I want (type is giving me the Context in this case).
NodeList result = doc.getElementsByTagName(type).item(0).getChildNodes();
But when I iterate over this, and pass each element to the StringWriter with the following, it isn't replacing the xi:include;
StringWriter sw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "no");
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty(OutputKeys.STANDALONE, "yes");
serializer.transform(new DOMSource(element), new StreamResult(sw));
String result = sw.toString();
Is there a way to do this with the JDK 7/8 built-in XML parsers, other than String manipulation to replace the xi:include myself?
Thanks for any assistance!