Reputation: 773
My requirement is to merge multiple xml files, where each file will be generated in a while loop and the merge the combined xml file to a root xml. I tried to follow below structure, but its not working. Can anyone please suggest is it the right approach. Also I cannot go for any xpath option since the xml which will be created will be dynamic each time...but the namespace will be same.
public Document xmlCreation(){
Document document1 = createDocument();
Node node1 = document1.getDocumentElement(); //Root node
//xml root node created
Document document2 = createDocument();
while(condition)
{
// some steps
Document document3 = createDocument();
Node node3 = document3.getDocumentElement(); //child node
// xml created
node3.appendChild(document2);
}
Node node2 = document2.getDocumentElement();
addChildNode(document1,node2);
return(document1);
}
Thank You in advance !!
Upvotes: 1
Views: 894
Reputation: 8058
In general, Nodes from one DOM Document can not be directly inserted into another Document. You need to run the importNode
operation on them to make copies which belong to the new Document, then insert those copies. See the discussion of ownerDocument issues in the DOM FAQ for a few more words on why and how, and see the discussion of importNode
in the DOM Recommendation's definition of the Document Node
If you're working in a Level 1 DOM, which predates the introduction of importNode
, you'll have to implement your own "create an equivalent node owned by the new Document" routine.
Upvotes: 0
Reputation: 3856
Try creating a resultDocument and use adoptNode(). This will move the nodes from the old documents to the new resultDocument. From the javadoc:
Attempts to adopt a node from another document to this document. If supported, it changes the ownerDocument of the source node, its children, as well as the attached attribute nodes if there are any. If the source node has a parent it is first removed from the child list of its parent. This effectively allows moving a subtree from one document to another (unlike importNode() which create a copy of the source node instead of moving it). When it fails, applications should use Document.importNode() instead.
Upvotes: 1
Reputation: 298539
You cannot add Node
s from another document. You have two options:
Document.adoptNode
to change the owner document to the target document. If this fails (i.e. returns null
) you have only option 2.Document.importNode
to create a copy of the node adapted to the target document/DOM implementation.Keep in mind that even then you cannot add a Document
to another Node
as you are trying to do in your example code. You can use the two options above to add the child nodes of the source document to your target node. What you typically want is to add the single root document element to another element.
Upvotes: 1
Reputation: 2771
use document.importNode(...)
.
Please refer to how does one go about copying one xml document's node to another? and http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.html#importNode(org.w3c.dom.Node,%20boolean) for more detail.
also, after importing the node, you should (still) add it as a child to the root node.
Upvotes: 0