Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I want to compare two XML files logically. That means that following text lines should be the same for the comparison tool:

1.

<test><hello/><test/>

2.

<test>




       <hello/>




<test/>

I'm a Windows developer and tried some comparison tools now: Beyond Compare, WinMerge, Total Commander, but all of them compare XML like a normal text comparison tool.

Are there any other tools or approaches I can take?

share|improve this question
1  
Check out the answer to this question on stackoverflow. Two suggested tools are XOM and XmlUnit. –  Jay Elston Aug 14 '11 at 0:42

4 Answers 4

Beyond Compare does work. Just download the XML tidied from here http://www.scootersoftware.com/download.php?zz=kb_moreformats_alt Also there are a couple other options as well.

share|improve this answer
    
+1 Interesting. Did not see that. I will try it. –  therealmarv Aug 14 '11 at 2:43
    
I tested XML tidied with attributes sorted. It compares XML very well. –  therealmarv Aug 14 '11 at 12:34

Most of the time, your language's standard xml libraries allow you to iterate through the nodes. If you just use the nodes, white-space is irrelevant anyways. All you need is one recursive function at that point and you are finished. I know in C#, for instance, you can load an XmlDocument and then grab each XmlNode of the document then the child nodes of each parent node. Java has the same capabilities.

share|improve this answer
    
I know that. But I'm looking for finished solutions to compare XML. –  therealmarv Aug 14 '11 at 2:44
    
@therealmarv Oh, well it would take less than an hour to write one if you can't find such a thing. If you'd like I'll write it for you and gpl it. –  Jonathan Henson Aug 14 '11 at 3:04

A simple groovy script follows that compares two strings to give you an idea of the things you can customise.

import org.custommonkey.xmlunit.XMLUnit;


def xml1 = 
    '''
    <test><hello/><data>text</data><data2>text2</data2><cdata>cdatatext</cdata></test>
    '''

def xml2 =
    '''
    <test>
        <hello/>



        <data>text</data>

        <data2>
        text2
        </data2>

        <cdata><![CDATA[cdatatext]]></cdata>

        <!-- comment -->

        </test>
    '''

// most likely comparison you want
XMLUnit.setIgnoreWhitespace(true)
XMLUnit.setIgnoreComments(true)
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true)
XMLUnit.setNormalizeWhitespace(true)

System.out.println(XMLUnit.compareXML(xml1, xml2))

Here is a version that compares two files passed as arguments

import org.custommonkey.xmlunit.XMLUnit;

def xml1 = new FileReader(args[0])
def xml2 = new FileReader(args[1])

XMLUnit.setIgnoreWhitespace(true)
XMLUnit.setIgnoreComments(true)
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true)
XMLUnit.setNormalizeWhitespace(true)

System.out.println(XMLUnit.compareXML(xml1, xml2))
share|improve this answer

One workaround would be to load both using the same XML parser, then have the parser write a new file with the same whitespace options -- this should effectively standardize the text output and let the text comparison tools work.

share|improve this answer
    
I'm already doing that. But this are extra steps everytime I want to compare two files. And sometimes comments in one XML do not destroy the logic of a XML. –  therealmarv Aug 13 '11 at 23:09
    
Could you then read in the files and compare the DOMs? –  Jeremy Heiler Aug 14 '11 at 0:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.