[英]how to compare two different XML with all tags and show the difference?
Expected XML预期的 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
<?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>
Output ------------ Expected rowsAffected 13 but rowsAffected is 14 Expected name is missing输出 ------------ 预期的 rowsAffected 13 但 rowsAffected 为 14 缺少预期的名称
There are 2 options有2个选项
a) Use a library like XMLUnit , get the differences and ignore the namespaces and compare text values a) 使用像 XMLUnit 这样的库,获取差异并忽略命名空间并比较文本值
b) Rollout a difference comparator of your own. b) 推出您自己的差异比较器。 If there only a specific tag you need to compare (and you do not want additional dependencies or JAXP) you can use this approach.如果您只需要比较一个特定的标签(并且您不想要额外的依赖项或 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;
}
The above code fails the assertion as expected and gives me the result上面的代码按预期失败了断言并给了我结果
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.