0

I have a very specific requirment of comparing 2 xml strings in java. I have 2 xml strings. Original and modified. I need to compare the original xml string with the modified and find out what has been modified.

For example:

Original xml is

   <Mycontacts>
        <contact>
           <firstName>Robert</firstName>
           <PhoneNumber>9053428756</PhoneNumber>
           <lastName>Bobbling</lastName>
           <mobile>4168014523</mobile>
        </contact>
        <contact> 

           <firstName>Lily</firstName>
           <PhoneNumber>9053428756</PhoneNumber>
           <lastName>Bobbling</lastName>
           <mobile>4168014523</mobile>
        </contact>
    </Mycontacts>

Modified xml:

    <Mycontacts>
        <contact>
           <firstName>Robert</firstName>
           <PhoneNumber>40454321333</PhoneNumber>
           <lastName>Bobbling</lastName>
           <mobile>4168014523</mobile>
        </contact>
      </Mycontacts>

As 1 contact is modified here and 1 id deleted I want to form 2 xml's trees. 1 is modify_xml and 1 is delete xml

modify xml:

     <contact>

            <firstName>Robert</firstName>
        <PhoneNumber>40454321333</PhoneNumber>
        <lastName>Bobbling</lastName>
           <mobile>4168014523</mobile>
        </contact>

delete xml:

     <contact>
           <name>Lily</name>
      </contact>

How can this be done using java API's? Is parsing each node and creating a map for each contact entry a good option?

edze
  • 2,965
  • 1
  • 23
  • 29
Coder
  • 91
  • 4
  • 12
  • in general you'd have to build a sorted tree to compare xml data, but in your case would a map be sufficient – Hachi Aug 31 '12 at 07:17

3 Answers3

1

http://xmlunit.sourceforge.net/

execc
  • 1,083
  • 12
  • 25
1

I would parse the XML files to Java objects and compare those, assuming that the XML layout is not changing over time. You can use XStream or JAXB to do that.

Kai
  • 38,985
  • 14
  • 88
  • 103
0

Very difficult problem in the general case, for example if you want to detect that the element names have changed but the values have stayed the same, or if you want to detect that two elements are both still present but the order has been reversed. It's a lot easier if you know something about the structure of your data, and for example you are able to distinguish which values act as identifiers, so the problem reduces to finding an element in the other file with the same identifier and then asking which of its non-identifying properties have changed.

The essential point is that you need to say a lot more about the requirements before one can attempt a design.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164