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) ref_doc.getDocumentElement();
org.apache.xerces.dom.NodeImpl test_root =
(org.apache.xerces.dom.NodeImpl) test_doc.getDocumentElement();
ref_root.normalize();
test_root.normalize();
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...
cheers,
Nathaniel Spurling
[EMAIL PROTECTED]
To: [EMAIL PROTECTED]
20.08.2002 12:53 cc:
Please respond to Subject: Re: Easily compare two
XML files
"Ant Developers
List"
First of all: I have the following links for you:
current JAXP, DOM, SAX javadoc
http://java.sun.com/xml/jaxp/index.html
XML Signature, XML canonicalization, and respective Java APIs
http://www.jcp.org/jsr/detail/105.jsp
http://www.w3.org/2000/09/xmldsig#
http://www.w3.org/Signature
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 documents
logically, you first have to canonicalize them. The attributes have to be
sorted alphanumerically etc.
The DOM API has method for canonicalization, but I suppose that they mainly
merge text nodes a.s.o.
Therefore I would in any case recommend to first read about the w3c insights
about document comparisons.
That might save you some headaches.
Of course, since an standard API for XML Signatures is currently in
developement at Sun, as the
Java Specification Request 105, you could also consider to wait on this API and
then just use
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. ;) )
greetings
Sascha Coenen
--
Nette Leute zum Flirten, Lachen und Verlieben,
fuer Reisen, Sport und Freizeit!
http://www.freenet.de/tipp/single
--
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: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>