1

I have 2 XML files as follows:

reference.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
    <name>Angels &amp; Demons</name>
    <isbn>9971-5-0210-0</isbn>
    <category></category>
</book>

comparison.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <books>
<book>
    <isbn>9971-5-0210-0</isbn>
    <author>Dan Brown</author>
    <category></category>
</book>

As you can see, there are tags in reference.xml that do not exist in comparison.xml and vice versa

I wan an output xml file of both files integrated as follows:

<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
    <name>Angels &amp; Demons</name>
    <isbn>9971-5-0210-0</isbn>
    <author>Dan Brown</author>
    <category></category>
</book>

I tried using XML unit (totally new in it) .. it's good in detecting the difference but not for integration .. so can you give me any ideas and whether I should use XML unit any further?

CodeX
  • 135
  • 2
  • 13
  • So the common column is the ISBN number? – Uma Kanth Jul 06 '15 at 12:51
  • I don't see how XMLUnit is relevant here. I'm afraid you'll have to write custom parser code to do this. – asgs Jul 06 '15 at 12:51
  • so use XML Unit to detect the difference and then on the basis of this detection, you can make your merged XML by writing your own parser. I dont think that there is any library for this – Freak Jul 06 '15 at 12:55

1 Answers1

3

I just googled and Found XMLMerge and it is working exactly the same you want. here you can see a tutorial. Lines copied from the given link

EL4J XmlMerge is a Java library under the LGPL license that makes it easier to merge elements from different XML sources. While XmlMerge is part of the EL4J framework, you can use it independently of EL4J. All you need to run the XmlMerge utility from your command line is JDK 1.5 or greater.

you can also find some answers on StackOverflow as well for example
Merge Two XML Files OR Merging XML Files
So as a final solution, you can detect a change by XMLUnit and then at then at end you can merge it using XMLMerge

Community
  • 1
  • 1
Freak
  • 6,786
  • 5
  • 36
  • 54
  • 1
    You should also read the [reference documentation PDF](http://el4j.sourceforge.net/docs/pdf/ReferenceDoc.pdf) found at http://el4j.sourceforge.net/documentation.html. It has a chapter dedicated to the XML merging capabilities. – Alin Pandichi Jul 06 '15 at 13:02
  • @freak Thanks alot .. the downloaded folder should contain a .jar file?? it does not exist – CodeX Jul 06 '15 at 13:07
  • when you will download el4j-3.1-convenience and unzip it then got to `docs` and then `pdf`. In `pdf` folder you will find many docs including the `SetupEL4J`. Please follow these documents. – Freak Jul 06 '15 at 13:16