从PL / SQl中的xml中提取元素

时间:2013-07-30 13:45:01

标签: xml oracle plsql xml-parsing extract

以下是SOAP服务的响应。我如何在Pl / SQL中访问值。我列出了一些我尝试过的方法

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
   <ShipmentTrackingResponse xmlns="http://ws.aramex.net/ShippingAPI/v1/">
    <Transaction xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
     <Reference1>001</Reference1>
     <Reference2 i:nil="true"/>
     <Reference3 i:nil="true"/>
     <Reference4 i:nil="true"/>
     <Reference5 i:nil="true"/>
    </Transaction>
    <Notifications xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/>   
    <HasErrors>false</HasErrors>
    <TrackingResults xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
     <a:KeyValueOfstringArrayOfTrackingResultmFAkxlpY>
      <a:Key>4738079651</a:Key>
      <a:Value>
      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH247</UpdateCode>
       <UpdateDescription>Supporting Document Returned to Shipper</UpdateDescription>
       <UpdateDateTime>2013-07-15T19:29:00</UpdateDateTime> 
       <UpdateLocation>Mumbai,India</UpdateLocation>
       <Comments/>
       <ProblemCode/>
      </TrackingResult>

      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH369</UpdateCode>
       <UpdateDescription>SMS Sent to Consignee</UpdateDescription>
       <UpdateDateTime>2013-07-12T09:10:00</UpdateDateTime>   
       <UpdateLocation>Mumbai,India</UpdateLocation>
       <Comments/>
       <ProblemCode/>
      </TrackingResult>

      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH033</UpdateCode>
       <UpdateDescription>Attempted Delivery - Payment Declined by Customer</UpdateDescription>
       <UpdateDateTime>2013-07-11T17:20:00</UpdateDateTime>
       <UpdateLocation>Goa Branch-GOI,India</UpdateLocation>
       <Comments/>
       <ProblemCode>A18</ProblemCode>
      </TrackingResult>

      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH369</UpdateCode>
       <UpdateDescription>SMS Sent to Consignee</UpdateDescription>
       <UpdateDateTime>2013-07-11T10:36:00</UpdateDateTime>
       <UpdateLocation>Mumbai,India</UpdateLocation>
       <Comments/>
       <ProblemCode/>
      </TrackingResult>

      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH003</UpdateCode>
       <UpdateDescription>Out for Delivery</UpdateDescription>
       <UpdateDateTime>2013-07-11T10:19:00</UpdateDateTime>
       <UpdateLocation>Goa Branch-GOI,India</UpdateLocation>
       <Comments/>
       <ProblemCode/>
      </TrackingResult>

      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH203</UpdateCode>
       <UpdateDescription>Record Created</UpdateDescription>
       <UpdateDateTime>2013-07-05T00:39:00</UpdateDateTime>
       <UpdateLocation>Nehru Place Branch,India</UpdateLocation>
       <Comments/>
       <ProblemCode/>
      </TrackingResult>
    </a:Value>
   </a:KeyValueOfstringArrayOfTrackingResultmFAkxlpY>
  </TrackingResults>
<NonExistingWaybills xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/></ShipmentTrackingResponse></s:Body>
</s: Envelope>

我如何提取价值,我实际上已经尝试了所有方法。 一些方法如下:

l_resp_xml := XMLType.createXML(l_clob_response);
SELECT EXTRACT(l_resp_xml, '//ShipmentTrackingResponse/TrackingResults',
 'xmlns="http://ws.aramex.net/ShippingAPI/v1/"') INTO l_resp_xml FROM dual;

最后提取值:但没用! 请帮助!!

SELECT EXTRACTVALUE(l_resp_xml, 
'//TrackingResults/a:KeyValueOfstringArrayOfTrackingResultmFAkxlpY/a:Value/TrackingResult[1]/UpdateDescription', 'xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"')INTO l_response_result FROM dual;
DBMS_OUTPUT.put_line ( 'Result> l_response_clobExtract=' || l_response_result);

如何使用XmlTable?

2 个答案:

答案 0 :(得分:2)

您需要关闭/到特定的XML标记/节点

 SELECT *
   FROM XMLTable('$dat//xmlpath' --the path to the node you want to start reading from
                   PASSING your_xml_node AS "dat"
                   COLUMNS
                       vcol NUMBER PATH 'col_path', --from your given node
                       ...etc
                    ) ;

对于您想要阅读的每个XML值,您需要在COLUMNS下添加一列并将其映射到给定XML节点中的路径

您可以在Oracle Docs

中阅读更多内容

编辑我使用了您在OP中列出的XML,以下是您可以执行的操作(我无法访问您的 http://ws.aramex.net/ShippingAPI/v1/ 架构,我使用*来查询任何架构/命名空间。

处理 XML 时最重要的重要是路径,你没有得到任何结果的原因是因为你在路径的某个地方出了问题。我花了一段时间才学到这一点,祝你好运:)

    SELECT *
      FROM XMLTABLE (
              xmlnamespaces (
                 'http://schemas.xmlsoap.org/soap/envelope/' AS "s",
                 'http://schemas.microsoft.com/2003/10/Serialization/Arrays' AS "a"),
              '$xd/s:Envelope/s:Body/*:ShipmentTrackingResponse/*:TrackingResults/a:KeyValueOfstringArrayOfTrackingResultmFAkxlpY'
              PASSING xmltype (
                         '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
   <ShipmentTrackingResponse xmlns="http://ws.aramex.net/ShippingAPI/v1/">
    <Transaction xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
     <Reference1>001</Reference1>
     <Reference2 i:nil="true"/>
     <Reference3 i:nil="true"/>
     <Reference4 i:nil="true"/>
     <Reference5 i:nil="true"/>
    </Transaction>
    <Notifications xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/>   
    <HasErrors>false</HasErrors>
    <TrackingResults xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
     <a:KeyValueOfstringArrayOfTrackingResultmFAkxlpY>
      <a:Key>4738079651</a:Key>
      <a:Value>
      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH247</UpdateCode>
       <UpdateDescription>Supporting Document Returned to Shipper</UpdateDescription>
       <UpdateDateTime>2013-07-15T19:29:00</UpdateDateTime> 
       <UpdateLocation>Mumbai,India</UpdateLocation>
       <Comments/>
       <ProblemCode/>
      </TrackingResult>

      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH369</UpdateCode>
       <UpdateDescription>SMS Sent to Consignee</UpdateDescription>
       <UpdateDateTime>2013-07-12T09:10:00</UpdateDateTime>   
       <UpdateLocation>Mumbai,India</UpdateLocation>
       <Comments/>
       <ProblemCode/>
      </TrackingResult>

      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH033</UpdateCode>
       <UpdateDescription>Attempted Delivery - Payment Declined by Customer</UpdateDescription>
       <UpdateDateTime>2013-07-11T17:20:00</UpdateDateTime>
       <UpdateLocation>Goa Branch-GOI,India</UpdateLocation>
       <Comments/>
       <ProblemCode>A18</ProblemCode>
      </TrackingResult>

      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH369</UpdateCode>
       <UpdateDescription>SMS Sent to Consignee</UpdateDescription>
       <UpdateDateTime>2013-07-11T10:36:00</UpdateDateTime>
       <UpdateLocation>Mumbai,India</UpdateLocation>
       <Comments/>
       <ProblemCode/>
      </TrackingResult>

      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH003</UpdateCode>
       <UpdateDescription>Out for Delivery</UpdateDescription>
       <UpdateDateTime>2013-07-11T10:19:00</UpdateDateTime>
       <UpdateLocation>Goa Branch-GOI,India</UpdateLocation>
       <Comments/>
       <ProblemCode/>
      </TrackingResult>

      <TrackingResult>
       <WaybillNumber>4738079651</WaybillNumber>
       <UpdateCode>SH203</UpdateCode>
       <UpdateDescription>Record Created</UpdateDescription>
       <UpdateDateTime>2013-07-05T00:39:00</UpdateDateTime>
       <UpdateLocation>Nehru Place Branch,India</UpdateLocation>
       <Comments/>
       <ProblemCode/>
      </TrackingResult>
    </a:Value>
   </a:KeyValueOfstringArrayOfTrackingResultmFAkxlpY>
  </TrackingResults>
<NonExistingWaybills xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/></ShipmentTrackingResponse></s:Body>
</s:Envelope>') AS "xd"
              COLUMNS key_col NUMBER PATH 'a:Key',
                      values_col XMLTYPE PATH 'a:Value') xx;

答案 1 :(得分:0)

感谢Jafar的帮助。我做了以下,它工作得很好!!它解决了.........

select x.UPDATEDESCRIPTION , x.UpdateDateTime into l_response_message, l_response_result from (select XMLType.createxml(l_clob_response) xml from dual) t, xmltable( xmlnamespaces ( 'http://ws.aramex.net/ShippingAPI/v1/' as "e", 'http://schemas.microsoft.com/2003/10/Serialization/Arrays' as "a", 'http://schemas.xmlsoap.org/soap/envelope/' as "s", default 'http://ws.aramex.net/ShippingAPI/v1/' ), 's:Envelope/s:Body/ShipmentTrackingResponse/TrackingResults/a:KeyValueOfstringArrayOfTrackingResultmFAkxlpY/a:Value/TrackingResult[1]' passing t.xml columns UpdateDescription varchar2(128) path 'UpdateDescription', UpdateDateTime varchar2(128) path 'UpdateDateTime' ) x; dbms_output.put_line('The UpdateDescription =>' || l_response_message); dbms_output.put_line('The UpdateDateTime =>' || l_response_result);