ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathaniel Spurling" <>
Subject Re: Easily compare two XML files
Date Tue, 20 Aug 2002 13:56:17 GMT

If you are using the Xerces parser there is an isEqualNode method,  you could try something
like this:

            InputSource test_xml;
            InputSource ref_xml;

                                javax.xml.parsers.DocumentBuilder db ;

                                Document ref_doc = db.parse(ref_xml);
                                Document test_doc = db.parse(test_xml);

                                org.apache.xerces.dom.NodeImpl ref_root = (org.apache.xerces.dom.NodeImpl)
                                org.apache.xerces.dom.NodeImpl test_root = (org.apache.xerces.dom.NodeImpl)


                                boolean same_doc = ref_root.isEqualNode(test_root, true)

The second parameter to isEqualNode is whether to perform a recursive comparison or not.

Of course it's not portable between parsers, since you cast to org.apache classes...


Nathaniel Spurling

                      20.08.2002 12:53         cc:                                       
                      Please respond to        Subject:  Re: Easily compare two XML files
                      "Ant Developers                                                    

First of all: I have the following links for you:

current JAXP, DOM, SAX javadoc

XML Signature, XML canonicalization, and respective Java APIs


My thoughts concerning XML document comparisons are          as follows:
The W3 consortium has developed a specification called XML Signature.
It deals with signing XML documents. As a prerequesite of being able to sign
a document, one must first develop a valid notion of comparing XML documents.

At first sight, your proposal seems to be quite logical. As you said, it does not
make sense to compare XML documents on byte level. What I want to contribute to your
thoughts is that comparing XML documents seems to be a non trivial thing per se.
Note, that the plan to just compare the element and attribute content is also an oversimplification.
For example: an element's attributes can appear in any order. Therefore, to compare to XML
logically, you first have to canonicalize them. The attributes have to be sorted alphanumerically

The DOM API has method for canonicalization, but I suppose that they mainly merge text nodes

Therefore I would in any case recommend to first read about the w3c insights about document
That might save you some headaches.

Of course, since an standard API for XML Signatures is currently in developement at Sun, as
Java Specification Request 105, you could also consider to wait on this API and then just
this API to compute the signatures of two XML docs you would like to compare and then just
compare the
two signature values.

I hope this was helpful to you.
(By the way, I am no Ant developer. So don't take my words for granted. ;) )

Sascha Coenen

Nette Leute zum Flirten, Lachen und Verlieben,
fuer Reisen, Sport und Freizeit!


This e-mail may contain confidential and/or privileged information. If you are not the intended
recipient (or have received this e-mail in error) please notify the sender immediately and
destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material
in this e-mail is strictly forbidden.

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message