简体   繁体   中英

Get a value from xml soap response?

Hi I have a java program that send this soap request:

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

I would suggest using JaxB to marshall your response to a java object then you can do whatever you want with the response

Create a JaxB object for the response:

@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). Please look into SAX Parser API available online. Here is the Link : 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#

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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