Miki
Miki

Reputation: 103

SOAP Response paring

I have the following soap response saved as an inline string. I just formatted it for easier reading:

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:getUserResponse xmlns:ns="http://www.cisco.com/AXL/API/10.0">
            <return>
                <user uuid="{SOME-IDENTIFIER-HERE}">
                    <firstName>MyFirstName</firstName>
                    <middleName />
                    <lastName>MyLastName</lastName>
                    <userid>myId</userid>
                    <password />
                    <pin />
                    <mailid>myEmail@myEmail.com</mailid>
                    <department />
                    <manager />
                    <userLocale />
                </user>
            </return>
        </ns:getUserResponse>
    </soapenv:Body>
</soapenv:Envelope>

How may I be able to parse through this and get specific element values inside this response? I was thinking of converting the string to an XML first and do parsing after.

Can anyone shed some light on how I could achieve this?

Thanks

Upvotes: 1

Views: 178

Answers (1)

TheCodingFrog
TheCodingFrog

Reputation: 3514

You can use XPath, e.g. below

import java.io.File;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class XPathTest {

    public static void main(String[] args) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new File("response.xml"));
        XPath xxPath = XPathFactory.newInstance().newXPath();
        String expression = "/Envelope/Body/getUserResponse/return/user";
        javax.xml.xpath.XPathExpression cc = xxPath.compile(expression);
        String result  = cc.evaluate(doc);
        System.out.println("Result:: " + result);
    }
}

response.xml

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:getUserResponse xmlns:ns="http://www.cisco.com/AXL/API/10.0">
            <return>
                <user uuid="{SOME-IDENTIFIER-HERE}">
                    <firstName>MyFirstName</firstName>
                    <middleName />
                    <lastName>MyLastName</lastName>
                    <userid>myId</userid>
                    <password />
                    <pin />
                    <mailid>myEmail@myEmail.com</mailid>
                    <department />
                    <manager />
                    <userLocale />
                </user>
            </return>
        </ns:getUserResponse>
    </soapenv:Body>
</soapenv:Envelope>

Output:

Result:: 
                    MyFirstName

                    MyLastName
                    myId


                    myEmail@myEmail.com

Upvotes: 1

Related Questions