Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to compare two similar xml documents ignoring node text values by using XMLUnit?

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to compare two similar xml documents ignoring node text values by using XMLUnit?

    I have two different XML documents below and please note that they are having the same basic structure (schema).

    Source XML

    <root>
    <name>String</name>
    <description>String</description>
    </root>

    Test XML

    <root>
    <name>Test</name>
    <description></description> <!-- it is an empty node -->
    </root>

    And I build this snippet function to compare those two XML documents.

    import org.custommonkey.xmlunit.Diff;
    import org.custommonkey.xmlunit.Difference;
    import org.custommonkey.xmlunit.IgnoreTextAndAttributeVal uesDifferenceListener;
    import org.custommonkey.xmlunit.XMLUnit;

    public static void main(String args[]) throws FileNotFoundException,
    SAXException, IOException, ParserConfigurationException, XPathExpressionException {

    String strSource = "<root><name>String</name><description>String</description></root>";
    String strTest = "<root><name>Test</name><description></description></root>";

    Document docSource = stringToXMLDocument(strSource);
    Document docTest = stringToXMLDocument(strTest);

    boolean result = isMatched(docSource, docTest);
    if(result){
    System.out.println("Matched!");
    }else{
    System.out.println("Un-matched!");
    }
    }
    public static boolean isMatched(Document xmlSource, Document xmlCompareWith) {
    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setIgnoreComments(true);
    XMLUnit.setIgnoreAttributeOrder(true);

    XMLUnit.setNormalizeWhitespace(true);
    XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

    Diff myDiff = new Diff(xmlSource, xmlCompareWith);
    myDiff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
    return myDiff.similar();
    }

    public static Document stringToXMLDocument(String str) throws ParserConfigurationException, SAXException, IOException{
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);

    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new InputSource(new StringReader(str)));

    return document;
    }


    And here is the Maven dependency

    <dependency>
    <groupId>xmlunit</groupId>
    <artifactId>xmlunit</artifactId>
    <version>1.6</version>
    </dependency>


    I am expecting those two XML documents are the same, but the function always returns false. Are there any ways that I can ignore the node text value when comparing two XML structures? As you can see, I already used IgnoreTextAndAttributeValuesDifferenceListener, but I still got the problem.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to compare two similar xml documents ignoring node text values by using XMLUnit?

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to compare two similar xml documents ignoring node text values by using XMLUnit?

    Somehow my post is denied here, I have posted detailed reply at javaranch

Similar Threads

  1. Trying to compare the values of 2 array indexes.
    By Siylo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 17th, 2019, 06:08 PM
  2. how to compare the large text files ?
    By vamshi in forum Java Theory & Questions
    Replies: 4
    Last Post: July 14th, 2013, 06:13 AM
  3. Replies: 2
    Last Post: July 9th, 2013, 09:51 PM
  4. [SOLVED] compare form values with database values
    By VaniRathna in forum Java Servlet
    Replies: 2
    Last Post: October 24th, 2011, 02:48 AM
  5. how to compare two set values
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: March 13th, 2010, 11:46 AM

Tags for this Thread