• 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

compare two xml files and adding extra nodes of second file to first file.

 
Ranch Hand
Posts: 68
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to xml files say abc.xml & 123.xml which are almost similar, i mean has the same content, but the second one i.e, 123.xml has more content than the earlier one. I want to read both the files using Java and to add the extra contents to abc.xml without changing its existing contents. HOw can i solve this problem using java?
 
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you could read the two documents into DOM structures and add the "extra" nodes from the second tree to the first tree, and then serialize the modified first tree back to an XML document again. I'm sure there are other options but this might be a straightforward way to do it.
 
Nidheesh Krishna
Ranch Hand
Posts: 68
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can i find what are the extra nodes to add?
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your question differ from the one you posted here? https://coderanch.com/t/664584/java/java/Java-Program-find-diff-xml
 
Nidheesh Krishna
Ranch Hand
Posts: 68
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, both questions are different
 
Paul Clapham
Marshal
Posts: 28175
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

Nidheesh Krishna wrote:how can i find what are the extra nodes to add?



I would have thought you already knew how to do that for your particular variety of XML documents. If you want something which compares two arbitrary XML documents and identifies nodes which are in one but not the other, then that's a very difficult problem. And then you're going to ask how to insert those "extra" nodes into the "right place" in the other document, which is also a very difficult problem.

So do you have an actual use case for this question? Do you have a set of documents for which you want to do this insertion-of-missing-nodes thing?
 
Paul Clapham
Marshal
Posts: 28175
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
If you really want to pursue this question, the first thing to do is to figure out how to tell if two nodes are different. Here are two documents:





So first you have the root nodes of the two documents. They are clearly different because their child nodes are in a different order. So the root node of the second document is not present in the first document. Now, where would you want to add that node into the first document? Remember that an XML document can only have a single root.
 
reply
    Bookmark Topic Watch Topic
  • New Topic