Java 获取 SoapBody 元素值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31355100/
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
Get SoapBody Element value
提问by Ahmet Karakaya
Here is the response I get from server,
Although I can take the MM7Version
element value, I cannot get the Status
element value. it returns null.
这是我从服务器得到的响应,虽然我可以获取MM7Version
元素值,但我无法获取Status
元素值。它返回空值。
SoapMeesage XML Response:
SoapMeesage XML 响应:
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Header>
<mm7:TransactionID SOAP-ENV:mustUnderstand="1" xmlns:mm7="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2">TID.BIP_TRASNID.20041103.135200.001</mm7:TransactionID>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<MM7Version>5.6.0</MM7Version>
<Status><StatusCode>2602</StatusCode><StatusText>Invalid status</StatusText></Status>
<MessageID></MessageID>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Java Code:
Java代码:
SOAPMessage response = connection.call(message, endpoint);
SOAPEnvelope env= response.getSOAPPart().getEnvelope();
SOAPBody sb = env.getBody();
Name ElName = env.createName("Status");
//Get child elements with the name XElement
Iterator it = sb.getChildElements(ElName);
//Get the first matched child element.
//We know there is only one.
SOAPBodyElement sbe = (SOAPBodyElement) it.next();
//Get the value for XElement
String MyValue = sbe.getValue();
System.out.println("MyValue: "+MyValue);
回答by Ahmet Karakaya
Coded a method by inspiring http://www.coderanch.com/t/640002/Web-Services/java/Error-parsing-SOAP-response-message
通过启发编写一个方法http://www.coderanch.com/t/640002/Web-Services/java/Error-parsing-SOAP-response-message
private static MmsResponse getMmsResponse(SOAPMessage response) throws SOAPException {
MmsResponse mmsResponse = new MmsResponse();
Iterator itr=response.getSOAPBody().getChildElements();
while (itr.hasNext()) {
Node node=(Node)itr.next();
if (node.getNodeType()==Node.ELEMENT_NODE) {
Element ele=(Element)node;
switch (ele.getNodeName()) {
case "MM7Version":
mmsResponse.setMm7Version(ele.getTextContent());
break;
case "MessageID":
mmsResponse.setMessageID(ele.getTextContent());
break;
case "Status":
NodeList statusNodeList = ele.getChildNodes();
Status status = new Status();
for(int i=0;i<statusNodeList.getLength();i++){
Element statusElement = (Element) statusNodeList.item(i);
switch (statusElement.getNodeName()) {
case "StatusCode":
status.setStatusCode(ele.getChildNodes().item(i).getTextContent());
break;
case "StatusText":
status.setStatusText(ele.getChildNodes().item(i).getTextContent());
break;
default:
break;
}
}
mmsResponse.setStatus(status);
break;
default:
break;
}
} else if (node.getNodeType()==Node.TEXT_NODE) {
//do nothing here most likely, as the response nearly never has mixed content type
//this is just for your reference
}
}
回答by Jason DSouza
Try this with the Conversion Boxcomponent ...
用转换框组件试试这个......
import java.util.Map;
import cjm.component.cb.map.ToMap;
public class Trying
{
public static void main(String[] args)
{
try
{
String parseXML = "<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><SOAP-ENV:Header><mm7:TransactionID SOAP-ENV:mustUnderstand='1' xmlns:mm7='http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2'>TID.BIP_TRASNID.20041103.135200.001</mm7:TransactionID></SOAP-ENV:Header><SOAP-ENV:Body><MM7Version>5.6.0</MM7Version><Status><StatusCode>2602</StatusCode><StatusText>Invalid status</StatusText></Status><MessageID></MessageID></SOAP-ENV:Body></SOAP-ENV:Envelope>";
Map<String, Object> parsedMap = new ToMap().convertToMap(parseXML); // you will need the conversion box component for this
Map<String, Object> envelopeMap = (Map<String, Object>) parsedMap.get("SOAP-ENV:Envelope");
Map<String, Object> bodyMap = (Map<String, Object>) envelopeMap.get("SOAP-ENV:Body");
String MM7Version = (String) bodyMap.get("MM7Version");
System.out.println("MM7Version = " + MM7Version);
Map<String, Object> statusMap = (Map<String, Object>) bodyMap.get("Status");
System.out.println("Status Code = " + statusMap.get("StatusCode"));
System.out.println("Status Text = " + statusMap.get("StatusText"));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Output
输出
-------- XML Detected --------
-------- Map created Successfully --------
MM7Version = 5.6.0
Status Code = 2602
Status Text = Invalid status