【问题标题】:How to get only XML content from a SOAP response如何从 SOAP 响应中仅获取 XML 内容
【发布时间】:2018-04-10 15:19:33
【问题描述】:

我有一个示例响应:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <trk:TrackResponse xmlns:trk="somens1">
            <common:Response xmlns:common="somens2">
                <common:ResponseStatus>
                    <common:Code>1</common:Code>
                    <common:Description>Success</common:Description>
                </common:ResponseStatus>
                <common:TransactionReference>
                    <common:CustomerContext>Sample Response</common:CustomerContext>
                </common:TransactionReference>
            </common:Response>

        </trk:TrackResponse>
    </soapenv:Body>
</soapenv:Envelope>

我想删除Envelope、Header 和Body 标记,只使用从TrackResponse 开始的xml。

我正在尝试使用jdom 遍历响应。

   Element body = jdom.getRootElement().getChild("Body", Namespace.getNamespace("http://schemas.xmlsoap.org/soap/envelope/"));

我实际上需要从 TrackResponse 开始的 jdom Element 检索到的 jdom Document 对象。

解决方案还是更好的替代方案?

【问题讨论】:

    标签: java xml soap jdom


    【解决方案1】:

    您的body 内容通过以下方式检索:

    Element body = jdom.getRootElement().getChild("Body", Namespace.getNamespace("http://schemas.xmlsoap.org/soap/envelope/"));
    

    做:

    Namespace trk = Namespace.getNamespace("somens1");
    Element response = body.getChild("TrackResponse", trk);
    response.detach();
    Document doc = new Document(response);
    

    这将创建一个以trk 为前缀的命名空间和TrackResponse 在根目录的新文档。将元素从一个位置(父级)移动到另一个位置时需要 detach()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      相关资源
      最近更新 更多