ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jlcarpen...@household.com
Subject RE: Easily compare two XML files (Suggestion for new ant utility)
Date Tue, 20 Aug 2002 16:51:13 GMT

Thanks for all the useful responses.  I didn't expect as many good answers
I received.  Quite obviously this must be something everyone else needs to
do as well.  With this amount of interest I now think some sort of XML
comparison would be a handy method to have available somewhere in the
org.apache.tools.ant.util package.

To shed a bit more light on the subject I will better describe the unit
test I am writting.

I am creating a pair of ant tasks that provide for importing largely
identical code from multiple sources into CVS, each on its own branch, and
then merging the differences into the main trunk.  To make testing easy I
am simply importing known test files (placed in src/etc/...) into a
temporary CVS instance.  I then run the cvstagdiff task to create XML
output I can compare to a saved expected output file.   As you might expect
all of this is driven by the unit test, including creating and eventually
destroying the temporary CVS repository.

Details relevant to the XML comparison are:
1) The ant task used to create the XML output is likely to have runtime
related data in it such as a timestamp.  (In the case of cvstagdiff this
doesn't happen.)
2) In the future the cvstagdiff task (or equiv.) is likely to output
additional elements not in my expected output.  In the case of my unit test
the most stable behavior is probably to ignore the new elements during the
comparison.
3) Comparing the XML files under the given constraints needs to be easy or
unit tests like mine will simply use a standard file comparison.

I would go ahead and make such a XML comparison utility but I am running
out of time.  I have already spent vastly more time than I expected dealing
with nasty bugs in the Execute and CVS tasks just to help get my tasks
working.

James Lee Carpenter
Software Engineer
Household Technical Services
6602 Convoy Court
San Diego, CA 92111

ph: 858-609-2461
email: jlcarpenter@household.com


                                                                                         
                                        
                                                                                         
                                        
                           Dominique Devienne          To:     "'Ant Developers List'" <ant-dev@jakarta.apache.org>
              
                           <DDevienne@lgc.com>         cc:                         
                                              
                                                       Subject:       RE: Easily compare two
XML files                            
                           08/20/2002 07:42 AM                                           
                                        
                           Please respond to                                             
                                        
                           "Ant Developers List"                                         
                                        
                                                                                         
                                        
                                                                                         
                                        



You could use http://sf.net/projects/diffxml, or write your own DOM-based
equals method for DOM nodes, as I did for testing purposes. Mine is
probably
not bug free, and deals with TEXT nodes specially, but it served me OK.
Here
the javadoc for it (it's only about 150 lines of code and documentation).

Cheers, --DD

  /**
   * Determines whether two DOM nodes are equals.
   * <p>
   * Two nodes are considered equals when they are of the same type,
   * have the same attributes (if any), and the same direct and indirect
   * children (if any).
   * <p>
   * <em>Note that this method cares about the order of the child
   * elements and attributes, so two documents having the same child
elements,
   * but in different order will not be considered equals!</em>
   * <p>
   * <em>Note on the other hand that two nodes need not have the
   * same parents, or belong to the same document, to be equals!</em>
   * <p>
   * <em>Finally, note that child text nodes containing only spaces
   * are ignored, and usually represent indentation in the XML
document!</em>
   *
   * @param  lhs the left-hand-side node to compare.
   * @param  rhs the right-hand-side node to compare.
   * @return <code>true</code> if equals; <code>false</code> otherwise.
   *         Two <code>null</code> nodes are considered equals.
   */
  public static boolean equals(Node lhs, Node rhs) {
    ...
  }

-----Original Message-----
From: jlcarpenter@household.com [mailto:jlcarpenter@household.com]
Sent: Monday, August 19, 2002 8:05 PM
To: Ant Developers List
Subject: Easily compare two XML files


I have an ant unit test that needs to easily compare two XML files.
I could simply do a standard file comparison, but it would seem a bit more
robust to compare the XML element values instead.

One thought is to make two DOM objects and see if the DOM object has an
appropriate equals method.
I haven't used an XML parser in Java for a year or so, and would like to
avoid spending an hour dragging up the appropriate documentation and
reminding myself of the exact syntax necessary.  Can anyone provide a few
helpers here.  Just knowing where to find documentation for whatever
version of Xerces is in the jar files distributed with Ant would be
helpful.

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>







--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message