如何使用PL / SQL提取随机soap xml有效负载

时间:2016-07-29 21:57:50

标签: sql xml oracle soap plsql

我有以下SOAP XML负载:

    <Errors>
        <Error Code="386" NodeList="FAX=+52 808 669.6271" RecordID="ROOT" 
               ShortText="Invalidfax number" Status="Not Processed" />
        <Error Code="1000386" NodeList="Contact Title=empty" RecordID="ROOT" ShortText="Contact Title is required" 
               Status="Not Processed" />
        <Error Code="311" NodeList="Contact First Name=empty" RecordID="ROOT" ShortText="Required data missing: first name"
               Status="Not Processed" />
        <Error Code="310" NodeList="Contact Last Name=empty" RecordID="ROOT" ShortText="Required data missing: last name" 
               Status="Not Processed" />
        <Error Code="316" NodeList="Contact Phone=empty" RecordID="ROOT" ShortText="Required data missing: phone number"
               Status="Not Processed" />
    </Errors>

要接收的错误记录数是随机的,从1到“N”。

当它只有#1错误记录时,我可以解析以下有效负载,执行:

        vxmlns := 'xmlns="http://www.opentravel.org/OTA/2003/05"';
        vCode :=  xmltype.getClobVal(vXml.extract('//Error/@Code', vxmlns));
        vNodeList :=  xmltype.getClobVal(vXml.extract('//Error/@NodeList', vxmlns));
        vShortText :=  xmltype.getClobVal(vXml.extract('//Error/@ShortText', vxmlns));
        vStatus :=  xmltype.getClobVal(vXml.extract('//Error/@Status', vxmlns));
        vType := xmltype.getClobVal(vXml.extract('//Error/@RecordID', vxmlns));

如何处理此有效负载以便单独处理每个错误记录?

理想的输出可能是:

Array[0]= "386 | FAX=+52 808 669.6271 | ROOT | Invalid fax number | Not Processed"
Array[1]= "1000386 |Contact Title=empty |ROOT| Contact Title is required |Not Processed"
.....
.....
Array[4]= "316 | Contact Phone=empty | ROOT | Required data missing: phone number | Not Processed" 

谢谢

1 个答案:

答案 0 :(得分:2)

您可以使用XMLTable query

select *
from XMLTable(
  '/Errors/Error'
  passing XMLType('<Errors>
        <Error Code="386" NodeList="FAX=+52 808 669.6271" RecordID="ROOT" 
               ShortText="Invalidfax number" Status="Not Processed" />
        <Error Code="1000386" NodeList="Contact Title=empty" RecordID="ROOT" ShortText="Contact Title is required" 
               Status="Not Processed" />
        <Error Code="311" NodeList="Contact First Name=empty" RecordID="ROOT" ShortText="Required data missing: first name"
               Status="Not Processed" />
        <Error Code="310" NodeList="Contact Last Name=empty" RecordID="ROOT" ShortText="Required data missing: last name" 
               Status="Not Processed" />
        <Error Code="316" NodeList="Contact Phone=empty" RecordID="ROOT" ShortText="Required data missing: phone number"
               Status="Not Processed" />
    </Errors>')
  columns code number path '@Code',
    nodeList varchar2(30) path '@NodeList',
    RecordID varchar2(10) path '@RecordID',
    shortText varchar2(40) path '@ShortText',
    status varchar2(20) path '@Status'
);

      CODE NODELIST                       RECORDID   SHORTTEXT                                STATUS             
---------- ------------------------------ ---------- ---------------------------------------- --------------------
       386 FAX=+52 808 669.6271           ROOT       Invalidfax number                        Not Processed       
   1000386 Contact Title=empty            ROOT       Contact Title is required                Not Processed       
       311 Contact First Name=empty       ROOT       Required data missing: first name        Not Processed       
       310 Contact Last Name=empty        ROOT       Required data missing: last name         Not Processed       
       316 Contact Phone=empty            ROOT       Required data missing: phone number      Not Processed       

更改passing子句以获取您的SOAP负载 - 您还没有显示它的来源,因此您必须自己调整它。您可以使选择列表将生成的列操作为您需要的任何格式,而不是使用select *