XML Including Files

Published: Sunday, 13 November 2005

How to merge many XML documents into one

We’ve identified two ways of doing this

ENTITY

Define an entity for each file you want to include within the DOCTYPE declaration.

<!DOCTYPE .....
  <!ENTITY contact SYSTEM "contact.xml">
>

Then you can refer to it using entity notation.

<contacts>
  &contact;
</contacts>

&contact; will automatically be expanded to the contents of the file “contact.xml”.

xi:include

Add xi:include elements to your XML. These will be processed by the XML parser.

<contacts>
 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="contact.xml"/>
</contacts>

If that does not work then you can try 2003 instead of 2001. The stands group changed it from 2001, to 2003 then back again. So depending on what version of Xerces you are using it might need 2003.

By using this method you can have contact.xml contain a doctype declaration and it will still be merged correctly.

You may also need to specify a system property to make the parser xi:include aware.

<sysproperty key="org.apache.xerces.xni.parser.XMLParserConfiguration"
   value="org.apache.xerces.parsers.XIncludeParserConfiguration"/>