Compare two XML strings ignoring element order

前端 未结 8 1611
陌清茗
陌清茗 2020-12-15 17:23

Suppose I have two xml strings


  a
  b



  b         


        
8条回答
  •  我在风中等你
    2020-12-15 17:26

    Just as an example of how to compare more complex xml elements matching based on equality of attribute name. For instance:

    
         
         
    
    

    vs.

    
         
         
    
    

    With following custom element qualifier:

    final Diff diff = XMLUnit.compareXML(controlXml, testXml);
    diff.overrideElementQualifier(new ElementNameAndTextQualifier() {
    
        @Override
        public boolean qualifyForComparison(final Element control, final Element test) {
            // this condition is copied from super.super class
            if (!(control != null && test != null
                          && equalsNamespace(control, test)
                          && getNonNamespacedNodeName(control).equals(getNonNamespacedNodeName(test)))) {
                return false;
            }
    
            // matching based on 'name' attribute
            if (control.hasAttribute("name") && test.hasAttribute("name")) {
                if (control.getAttribute("name").equals(test.getAttribute("name"))) {
                    return true;
                }
            }
            return false;
        }
    });
    XMLAssert.assertXMLEqual(diff, true);
    

提交回复
热议问题