简体   繁体   English

从 xml 肥皂响应中获取值?

[英]Get a value from xml soap response?

Hi I have a java program that send this soap request:你好我有一个发送这个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>

and I get this response:我得到了这个回应:

SOAPMessage soapResponse = soapConection.call(soapMessage, Properties.URL);
soapResponse.writeTo(System.out);

When I print show this:当我打印显示这个:

<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>

How I can get the id value in java (is an integer value).我如何在 java 中获取 id 值(是一个整数值)。

I would suggest using JaxB to marshall your response to a java object then you can do whatever you want with the response 我建议使用JaxB将您对Java对象的响应编组,然后您可以对响应进行任何操作

Create a JaxB object for the response: 为响应创建一个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;
    }
}

and then Marshall the response you have to the object 然后马歇尔对对象的响应

    JAXBContext jc = JAXBContext.newInstance(BuyerResponse.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();

    JAXBElement<BuyerResponse> je = unmarshaller.unmarshal(soapResponse.getSOAPBody().extractContentAsDocument(), BuyerResponse.class);

    BuyerResponse value = je.getValue();

Once you have got the response,You can SAX or DOM Parser API to parse the XML Elements and store values using constructors(Getter and Setter). 获得响应后,您可以使用SAX或DOM Parser API解析XML元素并使用构造函数(Getter和Setter)存储值。 Please look into SAX Parser API available online. 请查看在线提供的SAX Parser API。 Here is the Link : http://www.javacodegeeks.com/2012/01/xml-parsing-using-saxparser-with.html 这是链接: http : //www.javacodegeeks.com/2012/01/xml-parsing-using-saxparser-with.html

Note: Get the response from stream and then parse and store values. 注意:从流中获取响应,然后解析和存储值。

How Can I GET ID Value From Soap XML In C#如何在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM