Hey, I have a set of XML files, that can be represented in a few different
ways. The first way, is as one big XML file:

everything.xml:
<root>
<categoryA/>
<categoryB/>
</root>

The other way is to break each category into it's own XML file as follows:

categoryA.xml:
<root>
<categoryA/>
</root>

categoryB.xml:
<root>
<categoryB/>
</root>

Now, I want to know if there is a way I can extract <categoryA/> and <categoryB/>
from the two seperate XML files and combine them into one document (like
the first XML file), so that I can just process the one document. I want
to do this without having to go through and manually copy the applicable
part of the tree by hand. I'm currently using the Apache Group's XERCES
XML Parser.

What I've tried to do so far is this:

DOMParser parser = new DOMParser();

// Create the total document
Document combinedDocument = new DocumentImpl();
combinedDocument.appendChild(
combinedDocument.createElement("root")
);

parser.parse("file:categoryA.xml");
Document document = parser.getDocument();
Node node = document.getDocumentElement();

if (node.getTagName().equals("root"))
{
NodeList children = node.getChildNodes();

for (int i=0; i<children.getLength(); i++)
{
Node clone = children.item(i).cloneNode(true);
* combinedDocument.getDocumentElement().appendChild(clone);
}
}

This seems logical to me, but obviously I'm breaking some rule that I don't
yet understand. When i=0, the line flagged above runtime errors with the
following:

org.apache.xerces.dom.DOMExceptionImpl: DOM005 Wrong Document
at org.apache.xerces.dom.ChildAndParentNode.internalInsertBefore(ChildAndParentNode.java:340)
at org.apache.xerces.dom.ChildAndParentNode.internalInsertBefore(ChildAndParentNode.java:322)
at org.apache.xerces.dom.NodeImpl.appendChild(NodeImpl.java:214)
at tools.xmlcc.main(xmlcc.java:310) <--- my code

Any ideas? I also tried this with the following line of code for declaring
the combined document (and got the same error):

Document combinedDocument = new DocumentImpl(
new DocumentTypeImpl(null, "root")
);

Any help would be appreciated!

Bryan