解析XML并存储到表中

时间:2016-04-19 08:53:48

标签: xml oracle xml-parsing

我有一个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并获取&#34; contactid&#34;,&#34; role&#34;,&#34; position&#34;的值。并且对于所有接触者来说更少,并将其存储在一张桌子中。 如何实现这一目标。

我尝试过使用

   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 个答案:

答案 0 :(得分:2)

XMLSequence - 已弃用。请尝试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'
        )

答案 1 :(得分:1)

您应该将customerOrganisation添加到XMLSequence中。 因为每个变更请求都有一些组织 它有效:

<junit/>