2013-11-25 2 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

문서에서 null이되는 위치를 지정할 수 있습니까? @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 

우리가 javax.xml.soap.*는 객체 XML 트리를 통과 할 U 걸릴 수 있습니다 SOAPResponse을 구문 분석합니다. 어쨌든 SOAP Body에서 요소를 파싱해야 할 수도 있습니다. 우리는 DOM 형식을 사용하여 이러한 매우 간단한 방식으로 구문 분석 할 수 있습니다.

+0

효과가있었습니다. 자식 노드를 검색하려고했지만 결코 가치가 없었습니다. 내가 코드를 디버깅 할 때 깨달았다. – yogsma

관련 문제