vs777
vs777

Reputation: 644

Get the value from a SOAP message in Java

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

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

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

Upvotes: 3

Views: 16859

Answers (2)

vs777
vs777

Reputation: 644

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

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

Upvotes: 0

aryn.galadar
aryn.galadar

Reputation: 735

Assuming you're using 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.

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.

Upvotes: 2

Related Questions