2013-11-14 218 views
1

我無法成功解析Java中的SOAP響應(使用Bonita Open Solution BPM)。 我有以下SOAP響應(搜索在IBM內容管理的文檔; SOAP響應返回1個匹配文檔)解析Java中的SOAP響應

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <ns1:RunQueryReply xmlns="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema" xmlns:ns1="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema"> 
     <ns1:RequestStatus success="true"></ns1:RequestStatus> 
     <ns1:ResultSet count="1"> 
      <ns1:Item URI="http://xxxx/CMBSpecificWebService/CMBGetPIDUrl?pid=96 3 ICM8 ICMNLSDB16 ICCSPArchivSuche59 26 A1001001A12D18B30015E9357518 A12D18B30015E935751 14 1087&amp;server=ICMNLSDB&amp;dsType=ICM"> 
       <ns1:ItemXML> 
        <ICCSPArchivSuche ICCCreatedBy="EBUSINESS\iccadmin" ICCCreatedDate="2012-04-18T10:51:26.000000" ICCFileName="Golem_Artikel.txt" ICCFolderPath="" ICCLastModifiedDate="2012-04-18T10:51:28.000000" ICCLibrary="Dokumente" ICCModifiedBy="EBUSINESS\iccadmin" ICCSharePointGUID="c43f9c93-a228-43f9-8232-06bdea4695d1" ICCSharePointVersion="1.0 " ICCSite="Archiv Suche" cm:PID="96 3 ICM8 ICMNLSDB16 ICCSPArchivSuche59 26 A1001001A12D18B30015E9357518 A12D18B30015E935751 14 1087" xmlns:cm="http://www.ibm.com/xmlns/db2/cm/api/1.0/schema"> 
        <cm:properties type="document"> 
         <cm:lastChangeUserid value="ICCCMADMIN"/> 
         <cm:lastChangeTime value="2012-04-18T11:00:15.914"/> 
         <cm:createUserid value="ICCCMADMIN"/> 
         <cm:createTime value="2012-04-18T11:00:15.914"/> 
         <cm:semanticType value="1"/> 
         <cm:ACL name="DocRouteACL"/> 
         <cm:lastOperation name="RETRIEVE" value="SUCCESS"/> 
        </cm:properties> 
        <cm:resourceObject CCSID="0" MIMEType="text/plain" RMName="rmdb" SMSCollName="CBR.CLLCT001" externalObjectName=" " originalFileName="" resourceFlag="2" resourceName=" " size="702" textSearchable="true" xsi:type="cm:TextObjectType"> 
         <cm:URL value="http://cmwin01.ebusiness.local:9080/icmrm/ICMResourceManager/A1001001A12D18B30015E93575.txt?order=retrieve&amp;item-id=A1001001A12D18B30015E93575&amp;version=1&amp;collection=CBR.CLLCT001&amp;libname=icmnlsdb&amp;update-date=2012-04-18+11%3A00%3A15.001593&amp;token=A4E6.IcQyRE6_QbBPESDGxK2;&amp;content-length=0"/> 
        </cm:resourceObject> 
        </ICCSPArchivSuche> 
       </ns1:ItemXML> 
      </ns1:Item> 
     </ns1:ResultSet> 
     </ns1:RunQueryReply> 
    </soapenv:Body> 
</soapenv:Envelope> 

我想獲得的文件名(ICCFileName =「Golem_Artikel.txt」 )和這個文件的URL(< 釐米:URL值=「http://cmwin01.ebusiness.local:9080/icmrm/ICMResourceManager/A10 ...)在字符串變量使用Java。要做到這一點(Can't process SOAP response,How to do the Parsing of SOAP Response)但沒有成功。以下是我嘗試的:

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 

// Clean response xml document 
responseDocumentBody.normalizeDocument(); 
// Get result node 
NodeList resultList = responseDocumentBody.getElementsByTagName("ICCSPArchivSuche"); 
Element resultElement = (Element) resultList.item(0); 
String XMLData = resultElement.getTextContent(); 

// Check for empty result 
if ("Data Not Found".equalsIgnoreCase(XMLData)) 
    return null; 


DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
InputSource inputSource = new InputSource(); 
inputSource.setCharacterStream(new StringReader(XMLData)); 
Document doc = documentBuilder.parse(inputSource); 
Node node = doc.getDocumentElement(); 
String result = doc.getNodeType(); 


return result; 

從博尼塔,我只獲得responseDocumentBody或responseDocumentEnvelope(org.w3c.dom.Document)作爲web服務響應。因此,我需要從SOAP Body導航到我的變量。如果有人能幫忙,我會很高興。

問候

+0

我提交了一個用vtd-xml(http://vtd-xml.sf.net)寫的答案, –

回答

1

如果你做了很多與此相關的工作,我會明確推薦使用JAXB,正如MGoron所建議的那樣。如果這僅僅是一個鏡頭,XPATH也可以很好地工作。

/* 
    * Must use a namespace aware factory 
    */ 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    dbf.setNamespaceAware(true); 
    Document doc = dbf.newDocumentBuilder().parse(...); 


    /* 
    * Create an XPath object 
    */ 
    XPath p = XPathFactory.newInstance().newXPath(); 

    /* 
    * Must use a namespace context 
    */ 
    p.setNamespaceContext(new NamespaceContext() { 

     public Iterator getPrefixes(String namespaceURI) { 
      return null; 
     } 

     public String getPrefix(String namespaceURI) { 
      return null; 
     } 

     public String getNamespaceURI(String prefix) { 
      if (prefix.equals("ns1")) 
       return "http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema"; 
      if (prefix.equals("cm")) 
       return "http://www.ibm.com/xmlns/db2/cm/api/1.0/schema"; 
      return null; 
     } 
    }); 

    /* 
    * Find the ICCSFileName attribute 
    */ 
    Node iccsFileName = (Node) p.evaluate("//ns1:ICCSPArchivSuche/@ICCFileName", doc, XPathConstants.NODE); 
    System.out.println(iccsFileName.getNodeValue()); 

    /* 
    * Find the URL 
    */ 
    Node url = (Node) p.evaluate("//ns1:ICCSPArchivSuche/cm:resourceObject/cm:URL/@value", doc, XPathConstants.NODE); 
    System.out.println(url.getNodeValue()); 
0
  1. 得到RunQueryReply模式
  2. 地圖XSD到Java類使用JAX-B
  3. 解組應答串到JAX-B類對象
0

下面是代碼在VTD-XML要做到這一點,它基本上由2個XPath查詢,每區選出一個結果......然而,代碼是強大的,因爲它不承擔這些查詢將返回非空結果...

import com.ximpleware.*; 

public class parseSOAP { 
    public static void main(String[] s) throws VTDException, Exception{ 
     VTDGen vg = new VTDGen(); 
     vg.selectLcDepth(5);// soap has deep nesting so set to 5 to speed up navigation 
     if (!vg.parseFile("d:\\xml\\soap2.xml", true)) 
      return; 
     VTDNav vn = vg.getNav(); 
     AutoPilot ap =new AutoPilot(vn); 

     //declare name space for xpath 
     ap.declareXPathNameSpace("ns", "http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema"); 
     ap.declareXPathNameSpace("ns1", "http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema"); 
     ap.declareXPathNameSpace("cm", "http://www.ibm.com/xmlns/db2/cm/api/1.0/schema"); 
     ap.declareXPathNameSpace("soapenv", "http://www.w3.org/2003/05/soap-envelope"); 


     ap.selectXPath("/soapenv:Envelope/soapenv:Body/ns1:RunQueryReply/ns1:ResultSet/ns1:Item/ns1:ItemXML//ICCSPArchivSuche/@ICCFileName"); 
     int i=0; 
     if ((i=ap.evalXPath())!=-1){ 
      System.out.println("file name ==>"+vn.toString(i+1)); 
     } 

     ap.selectXPath("/soapenv:Envelope/soapenv:Body/ns1:RunQueryReply/ns1:ResultSet/ns1:Item/ns1:ItemXML//ICCSPArchivSuche/cm:resourceObject/cm:URL/@value"); 
     if ((i=ap.evalXPath())!=-1){ 
      System.out.println("file name ==>"+vn.toString(i+1)); 
     } 
    } 
}