如何使用pl/sql解析具有href标签的soap响应

Posted

技术标签:

【中文标题】如何使用pl/sql解析具有href标签的soap响应【英文标题】:How to parse soap response having href tag using pl/sql 【发布时间】:2013-02-13 03:51:30 【问题描述】:

我想解析下面的肥皂响应。问题出在 tag1 中,它指向一个名为 id1 的 href 链接。所以我们要到id1的response去获取value。

还有一件事是这个soap响应值是改变每个请求中的顺序。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
    <ns1:getBlock soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://diamondip.com/ipcontrol/ws/">
        <getBlockByIpAddressReturn href="#id0"/>
    </ns1:getBlock>

<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:WSGenericBlock" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://service.ipcontrol.diamondip.com">
    <tag1 href="#id1"/>      
    <tag2 soapenc:arrayType="soapenc:string[1]" xsi:type="soapenc:Array">
        <item xsi:type="soapenc:string">Container1</item>
    </tag2>  
    <tag3 xsi:type="xsd:dateTime">2013-02-01T06:45:32.000Z</tag3>
    <tag4 soapenc:arrayType="soapenc:string[4]" xsi:type="soapenc:Array">
        <item xsi:type="soapenc:string">uf1=221805</item>
        <item xsi:type="soapenc:string">uf2=34531</item>
    </tag4> 
    <tag5 href="#id2"/> 
</multiRef>   
<multiRef id="id1" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:boolean" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">false</multiRef>
<multiRef id="id2" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" lowestSetBit="3" xsi:type="soapenc:integer" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">8</multiRef>
</soapenv:Body>
</soapenv:Envelope>

【问题讨论】:

【参考方案1】:

也许这可以给你一个开始(这并不完美,我知道...)

declare

  xl clob := '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
    <ns1:getBlock soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://diamondip.com/ipcontrol/ws/">
        <getBlockByIpAddressReturn href="#id0"/>
    </ns1:getBlock>

<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:WSGenericBlock" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://service.ipcontrol.diamondip.com">
    <tag1 href="#id1"/>      
    <tag2 soapenc:arrayType="soapenc:string[1]" xsi:type="soapenc:Array">
        <item xsi:type="soapenc:string">Container1</item>
    </tag2>  
    <tag3 xsi:type="xsd:dateTime">2013-02-01T06:45:32.000Z</tag3>
    <tag4 soapenc:arrayType="soapenc:string[4]" xsi:type="soapenc:Array">
        <item xsi:type="soapenc:string">uf1=221805</item>
        <item xsi:type="soapenc:string">uf2=34531</item>
    </tag4> 
    <tag5 href="#id2"/> 
</multiRef>   
<multiRef id="id1" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:boolean" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">false</multiRef>
<multiRef id="id2" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" lowestSetBit="3" xsi:type="soapenc:integer" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">8</multiRef>
</soapenv:Body>
</soapenv:Envelope>';

  v_parser Xmlparser.Parser;
  v_doc    XMLDOM.DOMDocument;
  v_nl     XMLDOM.DOMNodeList;
  v_att    varchar2(300);
  v_refId  varchar2(300);
  v_val    varchar2(300);

begin

  v_parser := Xmlparser.newParser;
  xmlparser.setValidationMode(v_parser, TRUE);

  Xmlparser.parseClob(v_parser, xl);

  v_doc := Xmlparser.getDocument(v_parser);

  v_nl  := xslprocessor.selectNodes(XMLDOM.makeNode(v_doc), '//tag1');
  v_att := XMLDOM.getAttribute((XMLDOM.makeElement(XMLDOM.item(v_nl, 0))),
                               'href');

  v_refId := substr(v_att, 2);

  v_nl := xslprocessor.selectNodes(XMLDOM.makeNode(v_doc),
                                   '//multiRef[@id="' || v_refId || '"]');

  v_val := XMLDOM.getNodeValue(XMLDOM.getFirstChild(XMLDOM.item(v_nl, 0)));

  dbms_output.put_line(v_val);

end;

【讨论】:

以上是关于如何使用pl/sql解析具有href标签的soap响应的主要内容,如果未能解决你的问题,请参考以下文章

使用 PL/SQL 解析 XML 输出 html 中特定标签的内容

我正在尝试在 PL/SQL 中解析 XML。我无法从标签中检索属性值,我做错了啥?

ORACLE PL SQL 从 SOAP XML 中提取值

在 PL/SQL 过程中解析具有名称空间的 XML 的未知数量节点?

如何在iphone中解析soap请求

Oracle Pl/SQl:具有中间结果的自定义函数