This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes how to compare two xml files? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "how to compare two xml files?" Watch "how to compare two xml files?" New topic
Author

how to compare two xml files?

Anantha kumar
Greenhorn

Joined: Aug 24, 2010
Posts: 3
Hi all,

I need to compare two xml files and validate whether all elements are same.I am using DOM api to parse the xml files.I have retrieved the values but i am getting stuck in comparison.
Kindly help out.

Regards,
Ananthakumar
mahesh shinde
Ranch Hand

Joined: Jul 24, 2009
Posts: 69

using some good xml parser like sax or DOM xml parsers
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

That's true, without a parser you won't be able to do anything with any XML document. But it isn't very helpful, as the parser does absolutely nothing to help you to compare two XML documents.

Ananthakumar: you haven't said what you want to get out of this comparison. Do you just want to know if the two documents differ? Or what?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
Trying to determine if two XML documents are identical in the infoset sense is a classic XML problem.

Some problems:
1. an empty element is the same as a start element followed immediately by an end element.
2. the order of attributes is not significant

A bit of googling found this attempt to create a Diff for XML documents.

I have not tried that toolkit, please let us know how it works if you try it.

Bill
Anantha kumar
Greenhorn

Joined: Aug 24, 2010
Posts: 3
Hi all
Thank you for your response about this topic.Actually whats my problem is i can get the data elements from one xml and store it into hashmap and can get all the elemens from another one xml and store it into another hashmap.But the problem is one xml is well formatted one and another one is not well formated.i dont know how to proceed hereafter.Please help me out in this.

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10240
    
168

Anantha kumar wrote:
Actually whats my problem is i can get the data elements from one xml and store it into hashmap and can get all the elemens from another one xml and store it into another hashmap.


Hmm, I'm not sure how that really works. What's the purpose of storing them in a HashMap? Is it stored just like some random Strings?

But the problem is one xml is well formatted one and another one is not well formated.i dont know how to proceed hereafter


How did you figure out that one xml is well formatted and the other is not?


[My Blog] [JavaRanch Journal]
Anantha kumar
Greenhorn

Joined: Aug 24, 2010
Posts: 3
Hi jaikiran,

For performance use only am using hashmap.My one xml file contains <details>213310873*|#1067*|#11588*|#2009-08-28T07:12:54.000000<details> tag.

another well formed one contains:
<details>
<no>213310873</n>
<no1>1067>
...
</details>

So i have to compare those values 213310873 from two files and chec whether its equal or not.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
You are not going to be able to use XML tools if you don't have an XML document. So either:

1. repair the ill-formatted document
-or-
2. write your own code to extract the elements/text you want to compare by normal Java String methods.

Your description of the problem seems to have changed - do you really want to test for equality of two documents or are you just checking for identity of one small data item?

Bill

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1381
try XMLUnit
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
or search for "xml diff"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to compare two xml files?