简体   繁体   中英

Parsing a SOAP response is returning null

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){}

document is returning with null. I have tried different ways to pass to InputSource, but document is still returning null. Any idea why this might be happening?

I just tried i could get the elements name and values .

        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

To parse SOAPResponse we can javax.xml.soap.* it may take u to traverse the object xml tree. Anyway we may need parse the elements from SOAP Body . we could parse these very simple manner using DOM format .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM