【问题标题】:How to parse an SOAP response with internal references using XPath in Java如何在 Java 中使用 XPath 解析带有内部引用的 SOAP 响应
【发布时间】:2012-01-27 11:19:54
【问题描述】:

我想用 XPath 解析 SOAP 消息,但在 XML 响应中是内部引用。 XPath 不能解决这些问题。是否可以在使用 XPath 之前自动解析引用,以便我可以使用 xpath 表达式,如“//get_referencesResponse/Result/source/item” 还是有更好的阅读文档的方法?

<SOAP-ENV:Body>
    <get_referencesResponse SOAP-ENC:root="1" id="i1">
        <Result href="#i2"/>
    </get_referencesResponse>
    <Result SOAP-ENC:root="0" id="i2">
        <source href="#i3"/>
        <malware href="#i4"/>
        [...]
    </Result>
    <source SOAP-ENC:arrayType="xsd:string[2]" SOAP-ENC:root="0" id="i3" xsi:type="SOAP-    ENC:Array">
        <item href="#i8"/>
        <item href="#i9"/>
    </source>
    <malware SOAP-ENC:arrayType="xsd:string[7]" SOAP-ENC:root="0" id="i4" xsi:type="SOAP-ENC:Array">
        <item href="#i10"/>
        <item href="#i11"/>
        <item href="#i12"/>
        [...]
    </malware>
    [...]

    <item SOAP-ENC:root="0" id="i8" xsi:type="xsd:string">address</item>
    <item SOAP-ENC:root="0" id="i9" xsi:type="xsd:string">network_prefix</item>
    <item SOAP-ENC:root="0" id="i10" xsi:type="xsd:string">md5</item>
    [...]

    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

【问题讨论】:

    标签: java xml soap xpath xml-parsing


    【解决方案1】:

    是否可以在使用 XPath 之前自动解析引用,所以 我可以使用 xpath 表达式,例如 "//get_referencesResponse/Result/source/item" 还是有更好的 如何阅读文档?

    使用

    /*/item[@id = substring(/*/Result/source/@href, 2)]
    

    基于 XSLT 的验证

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/">
         <xsl:copy-of select=
         "/*/item[@id = substring(/*/Result/source/@href, 2)]"/>
     </xsl:template>
    </xsl:stylesheet>
    

    当此转换应用于以下 XML 文档时(您的文档,但格式正确,并添加了 itemid=3):

    <SOAP-ENV:Body
     xmlns:SOAP-ENV="some:SOAP-ENV"
     xmlns:SOAP-ENC="some:SOAP-ENC"
     xmlns:xsi="some:xsi">
        <get_referencesResponse SOAP-ENC:root="1" id="i1">
            <Result href="#i2"/>
        </get_referencesResponse>
        <Result SOAP-ENC:root="0" id="i2">
            <source href="#i3"/>
            <malware href="#i4"/>         [...]     
        </Result>
        <source SOAP-ENC:arrayType="xsd:string[2]"
        SOAP-ENC:root="0" id="i3" xsi:type="SOAP-ENC:Array">
            <item href="#i8"/>
            <item href="#i9"/>
        </source>
        <malware SOAP-ENC:arrayType="xsd:string[7]"
        SOAP-ENC:root="0" id="i4" xsi:type="SOAP-ENC:Array">
            <item href="#i10"/>
            <item href="#i11"/>
            <item href="#i12"/>         [...]     
        </malware>     [...]      
        <item SOAP-ENC:root="0" id="i3"
        xsi:type="xsd:string">name</item>
        <item SOAP-ENC:root="0" id="i8"
        xsi:type="xsd:string">address</item>
        <item SOAP-ENC:root="0" id="i9"
        xsi:type="xsd:string">network_prefix</item>
        <item SOAP-ENC:root="0" id="i10" x
        si:type="xsd:string">md5</item>     [...]      
    </SOAP-ENV:Body>
    

    计算 Xpath 表达式并输出所选元素

    <item xmlns:SOAP-ENV="some:SOAP-ENV"
     xmlns:SOAP-ENC="some:SOAP-ENC"
     xmlns:xsi="some:xsi"
     SOAP-ENC:root="0" id="i3" xsi:type="xsd:string">name</item>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多