如何从SOAP响应中获取单个元素的值? (JAVA)

时间:2022-11-27 17:33:46

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

我想从以下InputStream获取“SearchResult”值:

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:

我一直在尝试使用DOM解析器,但遇到了麻烦,并且不确定它是否是正确的方法:

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?

我怎样才能做到这一点?

1 个解决方案

#1


1  

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

对于Java和Android级别8或更高版本:nodeList.item(0).getTextContent()

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

对于android级别1和更高版本:nodeList.item(0).getFirstChild()。getNodeValue()

#1


1  

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

对于Java和Android级别8或更高版本:nodeList.item(0).getTextContent()

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

对于android级别1和更高版本:nodeList.item(0).getFirstChild()。getNodeValue()