从 Java 中的 SOAP 消息中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16071050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:48:50  来源:igfitidea点击:

Get the value from a SOAP message in Java

javaparsingsoap

提问by vs777

I am getting a SOAP message as a string after making a call to the web service.

在调用 Web 服务后,我收到了一个字符串形式的 SOAP 消息。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <PassOracleXMLDataResponse xmlns="http://tempuri.org/">
            <PassOracleXMLDataResult>
                <gesystem xmlns="">
                    <return_code>0</return_code>
                    <message>PRS User does not exists in GETS</message>
                    <invoiceid>TESTADDTLINFO2</invoiceid>
                    <datetime>Apr 17 2013  4:19PM</datetime>
                </gesystem>
            </PassOracleXMLDataResult>
        </PassOracleXMLDataResponse>
    </soap:Body>
</soap:Envelope>

I need to retrieve the values and elements. When I tried to use a simple SAXBuilder to build a Document and traverse it, I got an exception after trying to getChild on "soap: Body"

我需要检索值和元素。当我尝试使用一个简单的 SAXBuilder 来构建一个 Document 并遍历它时,我在“soap: Body”上尝试 getChild 后出现异常

getChild("soap:Body") - returns null.

采纳答案by aryn.galadar

Assuming you're using JDOM:

假设您使用的是 JDOM:

soap:Body is actually two parts: the namespace and the element name. You'll want to use the Namespace class to include that information when retrieving it.

soap:Body 实际上是两部分:命名空间和元素名称。在检索信息时,您需要使用 Namespace 类来包含该信息。

Try doing something like:

尝试执行以下操作:

envelopeNode.getChild("Body",envelopeNode.getNamespace());

That'll make it look for the child element with the name "Body", and the same namespace as the envelope node.

这将使它寻找名为“Body”的子元素,并且与包络节点具有相同的命名空间。

回答by vs777

Thanks Peter, it worked. There is only one strange thing. When I was getting a child Element for "PassOracleXMLDataResult" it also required to provide a Namespace as a second parameter

谢谢彼得,它奏效了。只有一件奇怪的事情。当我为“PassOracleXMLDataResult”获取子元素时,它还需要提供一个命名空间作为第二个参数

                Namespace nmspc = Namespace.getNamespace("http://tempuri.org/");    
            Element parseResponse = bodyEm.getChild("PassOracleXMLDataResponse", nmspc);            
            Element passResult = parseResponse.getChild("PassOracleXMLDataResult", nmspc);