2013-11-25 125 views
0
String response = "<?xml version='1.0'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'><soap:Body><exch:Response xmlns:exch='http://applicant.ffe.org/exchange/1.0'>...</exch:Response></soap:Body></soap:Envelope>"; 

DocumentBuilderFactory dbf = null; 
DocumentBuilder db = null; 
org.w3c.dom.Document document = null; 
try { 
    dbf = DocumentBuilderFactory.newInstance(); 
    db = dbf.newDocumentBuilder(); 
    InputSource is = new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8"))); 
    document = db.parse(is); 

} catch(ParserConfigurationException e){} 
    catch(SAXException e){} 

文檔返回null。我嘗試過不同的方式傳遞給InputSource,但文檔仍然返回null。任何想法爲什麼這可能會發生?解析SOAP響應返回null

+0

可以指定你在文檔中的空位置嗎? @yogsma – gks

回答

1

我剛試過,我可以得到元素的名稱和值。

 try { 
        dbf = DocumentBuilderFactory.newInstance(); 
        db = dbf.newDocumentBuilder(); 
        InputSource is = new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8"))); 
        document = db.parse(is); 
        System.out.println(document);//here we get null; 
         System.out.println(document.getNodeName());//here we get document; 
         for(int i =0 ; i<document.getChildNodes().getLength();i++) 
        System.out.println(document.getChildNodes().item(i).getChildNodes().item(i).getNodeName()); 
       } 


    Output : 

    [#document: null] 
    document 
    soap:Body 

爲了解析SOAPResponse我們可以javax.xml.soap.*它可能會把你遍歷對象XML樹。無論如何,我們可能需要解析SOAP Body中的元素。我們可以使用DOM格式解析這些非常簡單的方式。

+0

它的工作。我試圖檢索子節點,它從來沒有任何價值。當我調試代碼時實現。 – yogsma