Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. How to compare two similar xml documents ignoring node text values by using XMLUnit?

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

Scheduled Pinned Locked Moved Java
xmljavadatabasetutorialquestion
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mr pakapun
    wrote on last edited by
    #1

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

    String
    String

    Test XML

    Test

    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 = "StringString";
    String strTest = "Test";

    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

    xmlunit
    xmlunit
    1.6

    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 IgnoreTextAndAttributeValuesDifferenceLis

    L 1 Reply Last reply
    0
    • M mr pakapun

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

      String
      String

      Test XML

      Test

      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 = "StringString";
      String strTest = "Test";

      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

      xmlunit
      xmlunit
      1.6

      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 IgnoreTextAndAttributeValuesDifferenceLis

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You are more likely to get help from the owners of the XMLUnit package.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups