如何使用Java API解析SOAP响应

时间:2018-01-10 13:31:38

标签: java xml web-services soap automation

我正在使用Karate API执行SOAP Web服务,并生成基于SOAP的响应。

然后我使用Java代码将响应写入XML文件 - 已成功创建文件。

以下是我的样本SOAP响应内容......

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns1:getMbrWksMembershipDetailsResponse xmlns:ns1="http://www.xxxxxxx.com/services/customersummary">
         <ns4:WksMembershipSummaryResponse xmlns:ns2="http://www.xxxxxxx.com/services/customersummary/contract" xmlns:ns3="http://www.xxxxx.com/eom" xmlns:ns4="http://www.xxxx.com/services/customersummary">
            <ns2:customerSummary>
               <ns2:address>
                  <ns2:city>SOUTH CHESTERFIELD</ns2:city>
                  <ns2:country>USA</ns2:country>
                  <ns2:isoCountryCode>US</ns2:isoCountryCode>
                  <ns2:line1>9998, N. MICHIGAN ROAD.</ns2:line1>
                  <ns2:postalCode>23834</ns2:postalCode>
                  <ns2:state>VA</ns2:state>
               </ns2:address>
               <ns2:allowPasswordChange>true</ns2:allowPasswordChange>
               <ns2:arpMember>false</ns2:arpMember>
               <ns2:brandCode>RCI</ns2:brandCode>
               <ns2:brandId>1</ns2:brandId>
               <ns2:companyCode>RCI</ns2:companyCode>
               ..........

我编写了一个用于获取标记值的java代码,例如名字。这是Java方法。

public static String getTagValue(String strResponseFile, String strTagName)
    {
        String fileLoc = "D:\\getMembershipDetailsResponse.xml";
      try
      {
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
          dbf.setNamespaceAware(false); 

          DocumentBuilder db = dbf.newDocumentBuilder(); 
          **Document doc = db.parse(new InputSource(new FileReader(fileLoc)));** 
          XPath xpath = XPathFactory.newInstance().newXPath(); 

          Node n1 = (Node)xpath.evaluate("//customerSummary/brandCode", doc.getDocumentElement(), XPathConstants.NODE); 
          System.out.println(n1.getNodeValue()); 

          Node c1 = (Node)xpath.evaluate("//customerSummary/firstName",doc.getDocumentElement(), XPathConstants.NODE); 
          System.out.println(c1.getNodeValue());
      }
      catch(Exception e)
      {

      }
        return "";
    }

我在上面的粗体 Java代码行中得到NULL。您能否就此提出宝贵意见来解决问题?

谢谢,

0 个答案:

没有答案