2016-04-19 93 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