SOAP响应配对

时间:2015-07-19 06:57:21

标签: java soap xml-parsing

我将以下soap响应保存为内联字符串。我只是将其格式化以便于阅读:

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

我怎样才能解析这个并获取此响应中的特定元素值? 我正在考虑首先将字符串转换为XML并在之后进行解析。

任何人都可以阐明如何实现这一目标吗?

由于

1 个答案:

答案 0 :(得分:1)

您可以使用XPath,例如以下

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>

输出:

Result:: 
                    MyFirstName

                    MyLastName
                    myId


                    myEmail@myEmail.com