![](/img/trans.png)
[英]How to get the value of an xml element from within a SOAP response? (Android)
[英]Get a value from xml soap response?
你好我有一个发送这个soap请求的java程序:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:gs="http://talosdigital.com/buyer">
<soapenv:Header/>
<soapenv:Body>
<gs:createBuyerRequest>
<gs:name>Carlos</gs:name>
<gs:lastname>henao</gs:lastname>
</gs:createBuyerRequest>
</soapenv:Body>
</soapenv:Envelope>
我得到了这个回应:
SOAPMessage soapResponse = soapConection.call(soapMessage, Properties.URL);
soapResponse.writeTo(System.out);
当我打印显示这个:
<SOAP-ENV:Envelope>
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<ns2:createBuyerResponse>
<ns2:id>8</ns2:id>
<ns2:response>Buyer Created</ns2:response>
</ns2:createBuyerResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我如何在 java 中获取 id 值(是一个整数值)。
我建议使用JaxB将您对Java对象的响应编组,然后您可以对响应进行任何操作
为响应创建一个JaxB对象:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"id",
"response"
})
@XmlRootElement(name = "createBuyerResponse")
public class BuyerResponse{
@XmlElement(name = "id", required = true)
protected int id;
@XmlElement(name = "response", required = true)
protected String response;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getResponse() {
return this.response;
}
public void setResponse(String response) {
this.response = response;
}
}
然后马歇尔对对象的响应
JAXBContext jc = JAXBContext.newInstance(BuyerResponse.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<BuyerResponse> je = unmarshaller.unmarshal(soapResponse.getSOAPBody().extractContentAsDocument(), BuyerResponse.class);
BuyerResponse value = je.getValue();
获得响应后,您可以使用SAX或DOM Parser API解析XML元素并使用构造函数(Getter和Setter)存储值。 请查看在线提供的SAX Parser API。 这是链接: http : //www.javacodegeeks.com/2012/01/xml-parsing-using-saxparser-with.html
注意:从流中获取响应,然后解析和存储值。
如何在 C# 中从 Soap XML 获取 ID 值
**<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:m0="http://schemas.compassplus.com/two/1.0/fimi_types.xsd"
xmlns:m1="http://schemas.compassplus.com/two/1.0/fimi.xsd">
<env:Body>
<m1:InitSessionRp >
<m1:Response NextChallenge="9FC90277" Response="1" Ver="16.16" Product="FIMI">
<m0:Id>1219956</m0:Id>
<m0:NeedCAPAuth>0</m0:NeedCAPAuth>
<m0:PasswordVersion>1</m0:PasswordVersion>
</m1:Response>
</m1:InitSessionRp>
</env:Body>
</env:Envelope>**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.