Expected 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>
Output ------------ Expected rowsAffected 13 but rowsAffected is 14 Expected name is missing
There are 2 options
a) Use a library like XMLUnit , get the differences and ignore the namespaces and compare text values
b) Rollout a difference comparator of your own. If there only a specific tag you need to compare (and you do not want additional dependencies or JAXP) you can use this approach.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.