Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] Comparing two semantically equal XML files

xom-interest AT

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: "Michael Kay" <mike AT>
  • To: "'Marcelo Schneider'" <marcelo.schneider AT>, <xom-interest AT>
  • Subject: Re: [XOM-interest] Comparing two semantically equal XML files
  • Date: Fri, 10 Oct 2008 14:10:26 +0100

Your XML examples aren't well-formed, which makes it difficult to help you
as we can't run your test cases.

Whitespace is significant: if you want to eliminate it from the comparison,
you'll have to do this by hand (e.g. by a transformation applied before

Attribute order, and the difference between <a></a> and <a/>, should not be

Order of elements is significant.

You might like to look at the saxon:deep-equal() function as an alternative

It takes a set of flags to define what features of the XML you consider
significant or insignificant, and there is also an option to get
explanations if the documents are deemed non-equal (though that doesn't seem
to be needed for your application). It can be called from XPath and is also
available directly from Java as the static method

Michael Kay

> -----Original Message-----
> From: xom-interest-bounces AT
> [mailto:xom-interest-bounces AT] On Behalf Of
> Marcelo Schneider
> Sent: 10 October 2008 12:59
> To: xom-interest AT
> Subject: [XOM-interest] Comparing two semantically equal XML files
> Good morning list.
> In our company we currently store some graphs in XML form:
> the client can create a graph in a Java applet, that is sent
> to the server and stored as a file. Currently, all the graph
> <-> XML transformation is done using JDom.
> What I need to do is, when a user opens an already existing
> graph and tries to save it, compare the new XML sent with the
> previously existing XML in the server, saving only if they
> are different (so changing dates are correct).
> Googling around for a while I found out that a possible way
> to do that without worrying with XML structure was to use
> Canonicalization, and that XOM could possibly help me with
> that. Is this assumption correct?
> Here is what I'm currently doing:
> | public boolean areSemanticallyEqual(String oldXML, String newXML) {
> | Builder builder = new Builder();
> | Document oldDoc =;
> | Document newDoc =;
> | // here canonicalize the old xml
> | ByteArrayOutputStream oldStream = new ByteArrayOutputStream();
> | Canonicalizer outputter = new Canonicalizer(oldStream , false);
> | outputter.write(oldDoc);
> | // here canonicalize the new xml
> | ByteArrayOutputStream newStream = new ByteArrayOutputStream();
> | Canonicalizer outputter = new Canonicalizer(newStream, false);
> | outputter.write(newDoc);
> | return Arrays.equals(oldStream.toByteArray(),
> newStream.toByteArray());
> | }
> However, the above code returns false when I compare XMLs
> like these (they all start with "<?xml version="1.0"
> encoding="UTF-8"?>"):
> xml1: "<tag1><tag2 id="1" name="a">Some
> Text</tag2></tag1><other tag />"
> xml2: "<tag1>\n\t<tag2 id="1" name="a">Some
> Text</tag2>\r</tag1><other tag />" (with new lines, tabs and
> carriage returns)
> xml3: "<tag1><tag2 id="1" name="a">Some Text</tag2></tag1><other
> tag></other tag>" (other valid xml representation)
> xml4: "<tag1><tag2 name="a" id="1">Some
> Text</tag2></tag1><other tag />"
> (attributes order reversed)
> xml5: "<other tag /><tag1><tag2 id="1" name="a">Some
> Text</tag2></tag1>"
> (yet another thing that looks the same, but I'm probably
> wrong assuming this should count as equal)
> I would expect the return to be always true, except for
> (maybe?) xml1 > xml5. Is this correct? Any hints are highly
> appreciated (:
> --
> Marcelo Frantz Schneider
> SIC - TCO - Tecnologia em Engenharia do Conhecimento DÍGITRO
> --
> Esta mensagem foi verificada pelo sistema de antivírus da Dígitro e
> acredita-se estar livre de perigo.
> _______________________________________________
> XOM-interest mailing list
> XOM-interest AT

Archive powered by MHonArc 2.6.24.

Top of Page