2016-04-19 72 views
0

我有一个XML如下解析XML并存储到一个表

<?xml version="1.0" encoding="utf-8"?> 
<cus:request xsi:schemaLocation="http://www.bt.com/btgs/solutions/message/customerRequest C:\SalesSchema_Latest\RequestManagementV1.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:sortarg="http://www.bt.com/btgs/solutions/library/SourceAndTarget" 
    xmlns:siteType="http://www.bt.com/btgs/solutions/library/SiteType" 
    xmlns:reqType="http://www.bt.com/btgs/solutions/library/RequestType" 
    <sourceSystem name="Resign Tool" type="LQTWebsite" /> 
    <targetSystem name="MESSIA" type="Sales Tools" /> 
    <changeRequest customerRequestRef="" BTProjectManagementRef=""> 
     <customerOrganisation orgID="ID000001" orgInternalID="29823" resignInstanceType="Source"> 
      <organisationName>BT PLC</organisationName> 
      <contactInfo contactID="ID000002"> 
       <role>CUSTOMER</role> 
       <position /> 
       <fullName /> 
       <telephone /> 
       <fax /> 
       <email /> 
       <contactAddress> 
        <room /> 
        <floor /> 
        <building /> 
        <streetNumber /> 
        <streetName /> 
        <locality /> 
        <city /> 
        <county-state /> 
        <country ISOCountryCode="" /> 
        <postcode /> 
       </contactAddress> 
      </contactInfo> 
     </customerOrganisation> 
     <customerOrganisation orgID="ID000003" orgInternalID="29823" resignInstanceType="Target"> 
      <organisationName>BT PLC</organisationName> 
      <contactInfo contactID="ID000004"> 
       <role>CUSTOMER</role> 
       <position /> 
       <fullName /> 
       <telephone /> 
       <fax /> 
       <email /> 
       <contactAddress> 
        <building /> 
        <streetNumber /> 
        <streetName /> 
        <city /> 
        <county-state /> 
        <country /> 
        <postcode /> 
       </contactAddress> 
      </contactInfo> 
     </customerOrganisation> 
     <billTo siteID="ID000005"> 
      <organisation> 
       <organisationRef>ID000003</organisationRef> 
      </organisation> 
      <bilingContact contactID="ID000006"> 
       <role>Billing</role> 
       <position /> 
       <fullName /> 
       <telephone /> 
       <fax /> 
       <email /> 
       <contactAddress> 
        <room /> 
        <floor /> 
        <building /> 
        <streetNumber /> 
        <streetName /> 
        <city /> 
        <county-state /> 
        <country ISOCountryCode="" /> 
        <postcode /> 
       </contactAddress> 
      </bilingContact> 
     </billTo> 
     <defaultContract contractID="ID000007" contractInternalID="547386" resignInstanceType="Source"> 
      <contractReference>BTIM789456</contractReference> 
      <contractDuration>1</contractDuration> 
      <billingFrequency>Monthly</billingFrequency> 
     </defaultContract> 
     <defaultContract contractID="ID000008" contractInternalID="547386" resignInstanceType="Target"> 
      <contractReference>BTIM789457</contractReference> 
      <contractDuration>1</contractDuration> 
      <billingFrequency>Monthly</billingFrequency> 
     </defaultContract> 
     <billOfMaterials> 
      <bomSiteItem siteID="ID000009" siteInternalID="1163642" BTSiteType="host"> 
       <organisation> 
        <organisationRef>ID000003</organisationRef> 
       </organisation> 
       <siteContact> 
        <role /> 
        <firstName /> 
        <lastName /> 
        <telephone /> 
        <mobile /> 
        <fax /> 
        <email /> 
       </siteContact> 
       <siteName>BIRMINGHAM_001</siteName> 
       <siteReference>ID000009</siteReference> 
       <siteAddress> 
        <room /> 
        <floor /> 
        <building>BT BUILDING</building> 
        <streetNumber>5</streetNumber> 
        <streetName>BRINDLEY PLACE</streetName> 
        <locality>BIRMINGHAM</locality> 
        <city>BIRMINGHAM</city> 
        <county-state>WEST MIDLANDS</county-state> 
        <country ISOCountryCode="">UNITED KINGDOM</country> 
        <postcode>B1 2BL</postcode> 
       </siteAddress> 
       <coordinates gridSystem="easting-and-northing"> 
        <x-coordinate /> 
        <y-coordinate /> 
       </coordinates > 
       <installationLocation locID="ID000010"> 
        <subPremise>FLOOR 3 ROOM 6</subPremise> 
       </installationLocation> 
       <siteItem lineItemID="ID000011"> 
        <itemContract contractID="ID000012"> 
         <matrixType>Fixed Link WAN</matrixType> 
         <contractNumber /> 
         <contractReference>BTIM789456</contractReference> 
         <contractDuration>1</contractDuration> 
         <contractSignedDate>2015-07-31</contractSignedDate> 
        </itemContract> 
        <orderItemAction majorOrderAction="resign" minorOrderAction="resign" /> 
        <itemQuantity>1</itemQuantity> 
        <prod:product xsi:type="prod:CPEproductType" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" instanceID="ID000013" internalInventoryID="7236799"> 
         <productID>803624</productID> 
         <productDescription>NORTEL CS1000/MERIDIAN</productDescription> 
         <aEndSite> 
          <siteRef>ID000009</siteRef> 
          <locRef>ID000010</locRef> 
          <installationLocation> 
           <cabinet /> 
           <room /> 
           <floor /> 
          </installationLocation> 
         </aEndSite> 
         <CPEProductSpecificDetails> 
          <param>TEST</param> 
          <stringValue>Test</stringValue> 
         </CPEProductSpecificDetails> 
        </prod:product> 
       </siteItem> 
       <siteItem lineItemID="ID000014"> 
        <itemContract contractID="ID000015"> 
         <matrixType>Fixed Link WAN</matrixType> 
         <contractNumber /> 
         <contractReference>BTIM789456</contractReference> 
         <contractDuration>1</contractDuration> 
         <contractSignedDate>2015-07-31</contractSignedDate> 
        </itemContract> 
        <orderItemAction majorOrderAction="resign" minorOrderAction="resign" /> 
        <itemQuantity>1</itemQuantity> 
        <prod:product xsi:type="prod:CPEitemType" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" instanceID="ID000016" internalInventoryID="7236800"> 
         <productID>720056</productID> 
         <productType /> 
         <productDescription>TMP-00003</productDescription> 
         <BTitemCode>711875</BTitemCode> 
         <installationCPEID>ID000013</installationCPEID> 
        </prod:product> 
       </siteItem> 

       <siteItem lineItemID="ID000143"> 
        <itemContract contractID="ID000144"> 
         <matrixType>Fixed Link WAN</matrixType> 
         <contractNumber /> 
         <contractReference>BTIM789456</contractReference> 
         <contractDuration>1</contractDuration> 
         <contractSignedDate>2015-07-31</contractSignedDate> 
        </itemContract> 
        <orderItemAction majorOrderAction="resign" minorOrderAction="resign" /> 
        <itemQuantity>1</itemQuantity> 
        <prod:product xsi:type="prod:serviceWrapType" instanceID="ID000145" internalInventoryID="7235310" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
         <ruleID>62522</ruleID> 
         <productID>319819</productID> 
         <productDescription>Standard Care</productDescription> 
         <aEndSite> 
          <siteRef>ID000009</siteRef> 
          <locRef>ID000010</locRef> 
          <installationLocation> 
           <cabinet /> 
           <room /> 
           <floor /> 
          </installationLocation> 
         </aEndSite> 
         <associatedCPEID>ID000067</associatedCPEID> 
         <serviceWrapProductSpecificDetails> 
          <param attributeID="3772">BT ALIAS</param> 
          <integerValue>7235310</integerValue> 
          <param attributeID="2655">CONTRACT LINK</param> 
          <integerValue>547386</integerValue> 
          <param attributeID="318">EQUIPMENT LINK</param> 
          <integerValue>7236817</integerValue> 
          <param attributeID="318">EQUIPMENT LINK</param> 
          <integerValue>7236817</integerValue> 
          <param attributeID="318">EQUIPMENT LINK</param> 
          <integerValue>7236817</integerValue> 
          <param attributeID="9553">IS THIS PART OF MANAGEMENT BUNDLE</param> 
          <stringValue>NO</stringValue> 
          <param attributeID="3771">SOURCE SYSTEM</param> 
          <stringValue>EXPEDIO</stringValue> 
         </serviceWrapProductSpecificDetails> 
        </prod:product> 
       </siteItem> 

      </bomSiteItem> 



     </billOfMaterials> 
    </changeRequest> 
</cus:request> 

我想分析整个XML并获得“的ContactID”,“角色”,“位置”和一些更多的价值为所有contactid并将其存储到一个表中。 这是如何实现的。

我一直在使用

set serveroutput on; 
DECLARE 
    x XMLType := XMLType(
    ' <changeRequest customerRequestRef="" BTProjectManagementRef=""> 
     <customerOrganisation orgID="ID000001" orgInternalID="29823" resignInstanceType="Source"> 
      <organisationName>BT PLC</organisationName> 
      <contactInfo contactID="ID000002"> 
       <role>CUSTOMER</role> 
       <position /> 
       <fullName /> 
       <telephone /> 
       <fax /> 
       <email /> 
       <contactAddress> 
        <room /> 
        <floor /> 
        <building /> 
        <streetNumber /> 
        <streetName /> 
        <locality /> 
        <city /> 
        <county-state /> 
        <country ISOCountryCode="" /> 
        <postcode /> 
       </contactAddress> 
      </contactInfo> 
     </customerOrganisation> 
     <customerOrganisation orgID="ID000003" orgInternalID="29823" resignInstanceType="Target"> 
      <organisationName>BT PLC</organisationName> 
      <contactInfo contactID="ID000004"> 
       <role>CUSTOMER</role> 
       <position /> 
       <fullName /> 
       <telephone /> 
       <fax /> 
       <email /> 
       <contactAddress> 
        <building /> 
        <streetNumber /> 
        <streetName /> 
        <city /> 
        <county-state /> 
        <country /> 
        <postcode /> 
       </contactAddress> 
      </contactInfo> 
     </customerOrganisation> 
     </changeRequest>'); 


BEGIN 
    For R In (
    SELECT ExtractValue(Value(p),'/changeRequest/customerOrganisation/contactInfo/role/text()') as name 

    FROM TABLE(XMLSequence(Extract(x,'/changeRequest'))) p 
    ) LOOP 

    dbms_output.put_line(r.name); 
    End Loop; 
    Exception 
    When Others then 
    dbms_output.put_line(sqlerrm); 
END; 

尝试,但它抛出一个异常ORA-19025:extractValue一起返回只有一个节点

回答

2

和XMLSequence的价值 - 是deprecated.Try XMLTABLE XMLTABLE

访问XML elmeent属性使用 “@attrname”

select * from xmltable('/changeRequest/customerOrganisation' passing XMLType(
    ' <changeRequest customerRequestRef="" BTProjectManagementRef=""> 
     <customerOrganisation orgID="ID000001" orgInternalID="29823" resignInstanceType="Source"> 
      <organisationName>BT PLC</organisationName> 
      <contactInfo contactID="ID000002"> 
       <role>CUSTOMER</role> 
       <position /> 
       <fullName /> 
       <telephone /> 
       <fax /> 
       <email /> 
       <contactAddress> 
        <room /> 
        <floor /> 
        <building /> 
        <streetNumber /> 
        <streetName /> 
        <locality /> 
        <city /> 
        <county-state /> 
        <country ISOCountryCode="" /> 
        <postcode /> 
       </contactAddress> 
      </contactInfo> 
     </customerOrganisation> 
     <customerOrganisation orgID="ID000003" orgInternalID="29823" resignInstanceType="Target"> 
      <organisationName>BT PLC</organisationName> 
      <contactInfo contactID="ID000004"> 
       <role>CUSTOMER</role> 
       <position /> 
       <fullName /> 
       <telephone /> 
       <fax /> 
       <email /> 
       <contactAddress> 
        <building /> 
        <streetNumber /> 
        <streetName /> 
        <city /> 
        <county-state /> 
        <country /> 
        <postcode /> 
       </contactAddress> 
      </contactInfo> 
     </customerOrganisation> 
     </changeRequest>') 
     columns orgID varchar2(100) path './@orgID' 
      , contactID varchar2(100) path './contactInfo/@contactID' 
      , role varchar2(100) path './contactInfo/role' 
     ) 
+0

这对我的作品谢谢!我从来没有想过XMLTable.Now我想创建一个过程,除了xml作为输入,并将值获取到一些变量,以便它可以插入到表中。 – Vishal5364

+0

如果我使用我的完整XML,它将引发错误“ORA-01704:字符串文字太长” – Vishal5364

+0

您的xml很长。你必须将整个XML分解成varcahr2 chunck并将这个块添加到clob变量中。如果我使用dbms_output.put_line(r.role), –

1

你应该添加到customerOrganisation和XMLSequence。 因为你有一些组织为每个变更请求 它的工作原理:

DECLARE 
    x XMLType := XMLType(
    ' <changeRequest customerRequestRef="" BTProjectManagementRef=""> 
     <customerOrganisation orgID="ID000001" orgInternalID="29823" resignInstanceType="Source"> 
      <organisationName>BT PLC</organisationName> 
      <contactInfo contactID="ID000002"> 
       <role>CUSTOMER</role> 
       <position /> 
       <fullName /> 
       <telephone /> 
       <fax /> 
       <email /> 
       <contactAddress> 
        <room /> 
        <floor /> 
        <building /> 
        <streetNumber /> 
        <streetName /> 
        <locality /> 
        <city /> 
        <county-state /> 
        <country ISOCountryCode="" /> 
        <postcode /> 
       </contactAddress> 
      </contactInfo> 
     </customerOrganisation> 
     <customerOrganisation orgID="ID000003" orgInternalID="29823" resignInstanceType="Target"> 
      <organisationName>BT PLC</organisationName> 
      <contactInfo contactID="ID000004"> 
       <role>CUSTOMER</role> 
       <position /> 
       <fullName /> 
       <telephone /> 
       <fax /> 
       <email /> 
       <contactAddress> 
        <building /> 
        <streetNumber /> 
        <streetName /> 
        <city /> 
        <county-state /> 
        <country /> 
        <postcode /> 
       </contactAddress> 
      </contactInfo> 
     </customerOrganisation> 
     </changeRequest>'); 

    BEGIN 
     For R In (
      SELECT EXTRACTVALUE(Value(p),'/customerOrganisation/contactInfo/role/text()') as role, 
      EXTRACTVALUE(Value(p),'/customerOrganisation/contactInfo/position/text()') as position, 
      EXTRACTVALUE(Value(p),'/customerOrganisation/contactInfo/@contactID') as contactID 
     FROM TABLE(XMLSequence(Extract(x,'/changeRequest/customerOrganisation'))) p 
     ) LOOP 

     dbms_output.put_line(r.role || ' ' || r.position || ' ' || r.contactID); 
     End Loop; 
     Exception 
     When Others then 
     dbms_output.put_line(sqlerrm); 
    END; 
+0

doesnt'给出任何值; – Vishal5364

+0

感谢,更新dbms_output.put_line –

+0

如果我使用我的完整XML,它会引发一个错误“字符串文字太长”。 xml大小是140000个字符。 – Vishal5364