• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Merge nodes of two xml's in one

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic