aspose file tools*
The moose likes XML and Related Technologies and the fly likes Merge nodes of two xml's in one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Merge nodes of two xml Watch "Merge nodes of two xml New topic
Author

Merge nodes of two xml's in one

Devesh Shekhar Gupta
Greenhorn

Joined: Nov 24, 2009
Posts: 2
Hi,
I have two differnet xmls - each with different sets of nodes.
ex reply1.xml has nodes like id, name age,account
reply2.xml has nodes like startdate,enddate.
I need to merge them in a single xml. Currently I am doing this

XMLNode ReplyNode1= super.returnXml(Id);
(This reads from repy1.xml)

XMLNode ReplyNode2= returnXml(Id);
(This reads from repy2.xml)

XMLNode finalReply = null;

finalReply = XMLNode.createRootElement();
finalReply = XMLNode.createElement(0 , ReplyNode1.asString(true).concat(ReplyNode2.asString(true)));

However what this doing is simply appending reply2.xml at the end of reply1.xml. So in the final xml structure is awkward- there is an xml version tag where 1st xml ends and where second starts

ex- <?xml version="1.0"?> <id>......................</account> <?xml version="1.0"?><startdate>................</enddate>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So obviously you can't do that. Let's look at the requirements more carefully then. (I didn't really see any requirements in your post, though, so I'm going with the title of the thread as your requirements.)

First of all, as you probably know an XML document can only have a single root element. So the result of merging two XML documents, each of which has a single root element, can itself have only a single root element. So what is it going to be? The root of the first document? The root of the second document? Something different?

Okay, let's suppose you have that requirement taken care of. Let's suppose you're generating a new root element for the result document. Then what is supposed to happen to the root elements of the two input documents. Do they get discarded? Or are they used as the only two children of the result document's root?

Okay. Let's suppose you have that requirement taken care of. Let's suppose that the root elements of the two input documents get discarded. Then presumably you want to take all of the child nodes of those two roots and make them be child nodes of the result document's root. What order do you want them to be in? First document's nodes then second document's nodes? Or something else? And do you want anything special done with whitespace text nodes?

Now I might have chosen the wrong questions to ask, because the questions I asked depended on the answers I guessed for earlier questions. But the point is that it's up to you to ask those questions. Right now you don't know what your requirements are, so you don't know what to do. In which case there is no point in writing code. Get the requirements written down first.
Devesh Shekhar Gupta
Greenhorn

Joined: Nov 24, 2009
Posts: 2
I have two xml's and need to merge them in third new xml.
The roots of both the xml's will be discarded and a new xml root will be generated.
The child elements of both the xml's will now be child elemnts of new root.
First the roots of first xml be added then that of second xml.Nothing special neds to be done about whitespace text nodes


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay. So create the new root node, then go through the child nodes of each of the two documents and copy them over to be child nodes of that root node. What's the problem with that?

(In your original post you did weird things like converting the nodes to strings and concatenating the strings. Don't do that. Just treat the nodes as nodes.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Merge nodes of two xml's in one