File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Java Program to find diff between two xml files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Program to find diff between two xml files" Watch "Java Program to find diff between two xml files" New topic
Author

Java Program to find diff between two xml files

Vidya Pen
Greenhorn

Joined: Jul 24, 2006
Posts: 3
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

Joined: Oct 29, 2001
Posts: 8843
    
    7

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?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Casper Maxwell
Ranch Hand

Joined: Aug 04, 2005
Posts: 88
You can try the tools/libraries available below:

http://www.java-tips.org/java-libraries/xml-diff-libraries/
Vinutha Prakash
Greenhorn

Joined: Nov 27, 2013
Posts: 1

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

Joined: Oct 13, 2005
Posts: 38107
    
  22
Welcome to the Ranch
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Java Program to find diff between two xml files
 
Similar Threads
from where should i import com.sun.xml.tree.XmlDocumentBuilder;
dtd tags
CDATA and PCDATA
Xerces question
how many root element can be defined in one dtd files