xom-interest AT lists.ibiblio.org
Subject: XOM API for Processing XML with Java
List archive
Re: [XOM-interest] Comparing two semantically equal XML files
- From: Marcelo Schneider <marcelo.schneider AT digitro.com.br>
- To: xom-interest AT lists.ibiblio.org
- Subject: Re: [XOM-interest] Comparing two semantically equal XML files
- Date: Fri, 10 Oct 2008 11:43:28 -0300
Just found a small bug in my test case - now different attribute order and different <a /> / <a></a> pass. My fault.
Order element isn't really significant in my use-case, anyway I just sorted them alphabetically before sending assuring they are always in the same order - not really the best solution, but probably the fastest one.
About whitespace, just found https://lists.ibiblio.org/sympa/arc/xom-interest/2005-December/002760.html and NormalizingFactory (on the examples). These appear to work just as expected, so I (hopefully) won't need something as robust as Saxonica.
Thanks a lot for the help!
--
Marcelo Frantz Schneider
SIC - TCO - Tecnologia em Engenharia do Conhecimento DÍGITRO TECNOLOGIA
Michael Kay escreveu:
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
canonicalizing).
Attribute order, and the difference between <a></a> and <a/>, should not be
significant.
Order of elements is significant.
You might like to look at the saxon:deep-equal() function as an alternative
approach.
http://www.saxonica.com/documentation/extensions/functions/deepequal.html
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
net.sf.saxon.functions.DeepEqual.deepEquals().
Michael Kay
http://www.saxonica.com/
-----Original Message-----
From: xom-interest-bounces AT lists.ibiblio.org [mailto:xom-interest-bounces AT lists.ibiblio.org] On Behalf Of Marcelo Schneider
Sent: 10 October 2008 12:59
To: xom-interest AT lists.ibiblio.org
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 = builder.build(oldXML);
| Document newDoc = builder.build(newXML);
| // 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 TECNOLOGIA
--
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 lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
--
Esta mensagem foi verificada pelo sistema de antivírus da Dígitro e
acredita-se estar livre de perigo.
-
[XOM-interest] Comparing two semantically equal XML files,
Marcelo Schneider, 10/10/2008
- Re: [XOM-interest] Comparing two semantically equal XML files, Elliotte Harold, 10/10/2008
-
Re: [XOM-interest] Comparing two semantically equal XML files,
Michael Kay, 10/10/2008
- Re: [XOM-interest] Comparing two semantically equal XML files, Marcelo Schneider, 10/10/2008
Archive powered by MHonArc 2.6.24.