• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing two xml documents.  RSS feed

 
Steve Killeen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got what appears to be a tricky problem, I need to compare two xml documents...sounds simple enough, but the sibling elements within the comparing document can be un-sequential i.e. siblings can occur in any order under the equivalent parent and sibling elements can be non-unique i.e. element <file> can have a sibling called <file>. For example a simple case would be:
Alpha Document:

<root>
<file>
<topic>
<topicCode>1234</topicCode>
<topicName>xml</topicName>
</topic>
</file>
<file>
<topic>
<topicCode>4321</topicCode>
<topicName>java</topicName>
</topic>
</file>
</root>

Beta document:
<root>
<file>
<topic>
<topicCode>4321</topicCode>
<topicName>java</topicName>
</topic>
</file>
<file>
<topic>
<topicCode>1234</topicCode>
<topicName>xml</topicName>
</topic>
</file>
</root>
I would like my code to evaluate these documents as being equivalent, and to handle much more complex examples of the above problem. Are there any suggestions to what conceptual approach I could take in solving this problem, or are there any API's that I've missed that would do the job?
Regards,
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Do you have to do it via Java??
I have done similar thing via XSLT though...
I found this
link...
regards
Maulin
 
Steve Killeen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maulin,
Thanks for your reply. Under pre-defined XML comparision XSLT would be a good choice, however, the XML that is getting returned by the application is very variable in both data and structure, and a java codeded solution is preferable.
I was thinking that I could create a linked list/tree structure where each node could be referenced within a two dimensional Vector. But that sounds like a lot of work.
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,
dom4j has a class org.dom4j.util.NodeComparator with a compare(Document n1, Document(n2)) method. I believe that it compares the values of the Nodes and not the Node text, so this probably is NOT what you want. But, you could take a look at the source at get an idea of how to write your own method. Does this help?

Barry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!