2016-10-15 44 views
-1

預期的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 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 預期的名稱是缺少

+0

的命名空間是相同的;你爲什麼需要忽略它? –

+0

是的命名空間是不同的bvoip vs ns,我需要比較只有值與標籤 –

+0

這是一個名稱空間**前綴**;兩個前綴的命名空間是相同的,即「http:// dbor.att.com/bvoip/v1」。鑑於名稱空間是相同的;你爲什麼要忽略它? –

回答

0

有2個選項

a)使用類似XMLUnit的庫,獲取diff並忽略名稱空間和比較文本值

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) 
+0

但標籤將在此處動態顯示String xPath =「// * [local-name()='rowsAffected']」; –

+0

以及如何顯示缺少的xpath標籤也命名爲JAK .. –

+0

中的預期響應 –

相關問題