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?

3
  • So the common column is the ISBN number? Commented Jul 6, 2015 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. Commented Jul 6, 2015 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 Commented Jul 6, 2015 at 12:55

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

You should also read the reference documentation PDF found at el4j.sourceforge.net/documentation.html. It has a chapter dedicated to the XML merging capabilities.
@freak Thanks alot .. the downloaded folder should contain a .jar file?? it does not exist
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.