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.


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

Results 1 to 2 of 2

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

  1. #1
    Junior Member
    Join Date
    Mar 2021
    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


    Test XML

    <description></description> <!-- it is an empty node -->

    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);
    public static boolean isMatched(Document xmlSource, Document xmlCompareWith) {


    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();

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

    return document;

    And here is the Maven 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
    Eastern Florida
    Thanked 2,618 Times in 2,568 Posts

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

    Also posted here: https://coderanch.com/t/739936/langu...ments-ignoring
    If you don't understand my answer, don't ignore it, ask a question.

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