如何比较两个不同的XML与所有标签,并显示差异?

时间:2016-10-15 14:35:02

标签: java xml web-services java-ee xpath

预期的XML

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <bvoip:updateCustSiteDetailResponse xmlns:bvoip="http://dbor.att.com/bvoip/v1">
<bvoip:rowsAffected>13</bvoip:rowsAffected>
<bvoip:name>JAK</bvoip:name>
</bvoip:updateCustSiteDetailResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope><!-- P-V : 2016.10.21 -->

TargetXML

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SOAP-ENV:Body>
<ns:updateCustSiteDetailResponse xmlns:ns="http://dbor.att.com/bvoip/v1">
      <ns:rowsAffected>14</ns:rowsAffected>
    </ns:updateCustSiteDetailResponse>  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

输出     ------------     预期的行受影响13但受影响的行数为14     缺少预期名称

1 个答案:

答案 0 :(得分:0)

有两个选项

a)使用类似XMLUnit的库,获取差异并忽略命名空间并比较文本值

b)推出自己的差异比较器。如果只需要比较特定标记(并且您不需要其他依赖项或JAXP),则可以使用此方法。

public static void main(String[] args) throws XPathExpressionException {
        String controlXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">  <SOAP-ENV:Header/>  <SOAP-ENV:Body>    <bvoip:updateCustSiteDetailResponse xmlns:bvoip=\"http://dbor.att.com/bvoip/v1\"><bvoip:rowsAffected>13</bvoip:rowsAffected><bvoip:name>JAK</bvoip:name></bvoip:updateCustSiteDetailResponse>  </SOAP-ENV:Body></SOAP-ENV:Envelope>";
        String resultXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">  <SOAP-ENV:Body><ns:updateCustSiteDetailResponse xmlns:ns=\"http://dbor.att.com/bvoip/v1\">      <ns:rowsAffected>14</ns:rowsAffected>    </ns:updateCustSiteDetailResponse>  </SOAP-ENV:Body></SOAP-ENV:Envelope>";
        String xPath = "//*[local-name()='rowsAffected']";
        XPath xPathFactory = XPathFactory.newInstance().newXPath();
        Document controlDocument = getXMLDocument(controlXML);
        Document resultDocument = getXMLDocument(resultXML);
        Assert.assertEquals(xPathFactory.compile(xPath).evaluate(controlDocument),
                xPathFactory.compile(xPath).evaluate(resultDocument));

    }

    static private Document getXMLDocument(String xml) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        Document document = null;
        try {
            builder = factory.newDocumentBuilder();
            document = builder.parse(new InputSource(new StringReader(xml)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return document;
    }

上面的代码没有按预期的那样断言,并给我结果

Exception in thread "main" junit.framework.ComparisonFailure: The text nodes do not match expected:<1[3]> but was:<1[4]>
    at junit.framework.Assert.assertEquals(Assert.java:81)
    at org.ram.so.SOProject.XMLCompare.main(XMLCompare.java:26)