[英]how to compare two different XML with all tags and show the difference?
預期的 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 -->
目標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>
輸出 ------------ 預期的 rowsAffected 13 但 rowsAffected 為 14 缺少預期的名稱
有2個選項
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.