Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Program to find diff between two xml files

 
Vidya Pen
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i need to write a Java Program that will create an xml file based on the difference between two xml file.
For eg: say i have two xml files 1.xml and 2.xml .

1.xml
-----

<?xml version= '1.0'?>
<!DOCTYPE ADDRESSBOOK [
<!ELEMENT ADDRESSBOOK (PERSON)*>
<!ELEMENT PERSON (FIRSTNAME, COMPANY)>
<!ELEMENT FIRSTNAME (#PCDATA)>
<!ELEMENT COMPANY (#PCDATA)>
]>
<ADDRESSBOOK>

<PERSON>
<FIRSTNAME>Doe</FIRSTNAME>
<COMPANY>Doe Enterprise, Inc.</COMPANY>
</PERSON>

<PERSON>
<FIRSTNAME>Doe</FIRSTNAME>
<COMPANY>Doe Enterprise, Inc.</COMPANY>
</PERSON>

</ADDRESSBOOK>

2.xml
-------

<?xml version= '1.0'?>
<!DOCTYPE ADDRESSBOOK [
<!ELEMENT ADDRESSBOOK (PERSON)*>
<!ELEMENT PERSON (FIRSTNAME,EMAIL)>
<!ELEMENT FIRSTNAME (#PCDATA)>
<!ELEMENT EMAIL (#PCDATA)>
]>
<ADDRESSBOOK>

<PERSON>
<FIRSTNAME>Doe</FIRSTNAME>
<EMAIL>john@doeenterprise.com</EMAIL>
</PERSON>

<PERSON>
<FIRSTNAME>Doe</FIRSTNAME>
<EMAIL>john@doeenterprise.com</EMAIL>
</PERSON>

</ADDRESSBOOK>

Now i need to generate a third file 3.xml with the output as follows(In this file the elements common to 1.xml and 2.xml are deleted.Also they should be deleted only if there are no child elements for them.So here the PERSON elememt should not be deleted)

3.xml
-----
<?xml version= '1.0'?>
<!DOCTYPE ADDRESSBOOK [
<!ELEMENT ADDRESSBOOK (PERSON)*>
<!ELEMENT PERSON (COMPANY, EMAIL)>
<!ELEMENT COMPANY (#PCDATA)>
<!ELEMENT EMAIL (#PCDATA)>
]>
<ADDRESSBOOK>

<PERSON>
<COMPANY>Doe Enterprise, Inc.</COMPANY>
<EMAIL>john@doeenterprise.com</EMAIL>
</PERSON>

<PERSON>
<COMPANY>Doe Enterprise, Inc.</COMPANY>
<EMAIL>john@doeenterprise.com</EMAIL>
</PERSON>

</ADDRESSBOOK>

I should provide these files (ie,2 input files and one output file) as command line arguments .

Please revert as soon as possible

Thanks and Regards
Vidya
 
Joe Ess
Bartender
Pie
Posts: 9257
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Vidya. You must have missed our naming policy on the way in, but we ask that everyone use a real-sounding name, both a first and last name separated by a space. It keeps things friendly around here.
As for your question, while we love to help people, we ask that you at least Show Some Effort. We aren't going to Do Your Homework, but we will help you solve problems along the way.
Have you written any code? Do you have any specific questions or problems we can help with?
 
Casper Maxwell
Ranch Hand
Posts: 88
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try the tools/libraries available below:

http://www.java-tips.org/java-libraries/xml-diff-libraries/
 
Vinutha Prakash
Greenhorn
Posts: 1
jQuery Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below blog link helps to get Diff b/w 2 xml in list of xpath format

http://jamesfarrugia.blogspot.com/2011/12/comparing-two-xml-documents-in-java.html

Using the above xpath list we can build xml output with the help of below code.




ArrayList<String> uniques are list of xpaths from the blog code.

Hope the above information helps...
Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic