简体   繁体   中英

How to get the value of a single element from a SOAP response? (Java)

I want to get the "SearchResult" value from the following InputStream:

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
    <soap12:Body>
        <SearchResponse xmlns="[url here]">
            <SearchResult>[THIS VALUE HERE]</SearchResult>
        </SearchResponse>
      </soap12:Body>
</soap12:Envelope>

I've been trying to use a DOM parser but am having trouble, and am not sure if it is the right approach anyway:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
Document doc = null;

db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(soapResponse));
doc = db.parse(inStream);  

NodeList nl = doc.getElementsByTagName("SearchResult");
//Not sure how to get the value... Everything I've tried returned "null". Help?

How can I do this?

For Java and Android level 8 or newer: nodeList.item(0).getTextContent()

For android level 1 and newer: nodeList.item(0).getFirstChild().getNodeValue()

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