[XOM-interest] Comparing two semantically equal XML files
Marcelo Schneider
marcelo.schneider at digitro.com.br
Fri Oct 10 07:59:04 EDT 2008
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.
More information about the XOM-interest
mailing list