Compare two XML strings ignoring element order

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

Suppose I have two xml strings


  a
  b



  b         


        
8条回答
  •  情书的邮戳
    2020-12-15 17:49

    XMLUnit will do what you want, but you have to specify the elementQualifier. With no elementQualifier specified it will only compare the nodes in the same position.

    For your example you want an ElementNameAndTextQualifer, this considers a node similar if one exists that matches the element name and it's text value, something like :

    Diff diff = new Diff(control, toTest);
    // we don't care about ordering
    diff.overrideElementQualifier(new ElementNameAndTextQualifier());
    XMLAssert.assertXMLEqual(diff, true);
    

    You can read more about it here: http://xmlunit.sourceforge.net/userguide/html/ar01s03.html#ElementQualifier

提交回复
热议问题