2014-01-07 68 views
2

我正在嘗試創建一個用列與xml文件分隔的oracle表。 我使用的SQL命令以創建表:基於XML文件創建Oracle表

create table mytable of XMLType; 

insert into mytable2 values(XMLType(bfilename('XMLDIR','XMLFileName.xml'),nls_charset_id('AL32UTF8'))); 

文件被插入到一個XML結構的表,但我需要查看在列或僅選擇字段到令牌克拉後標記: IID。 例如,在第一個標籤中,我需要選擇值「OrphanedBSCs」。

<?xml version="1.0" encoding="WINDOWS-1252"?> 
<ArrayOfRootEntityType xmlns:umts="http://www.aircominternational.com/Schemas/UMTS/2010/07" xmlns:tra70="http://www.aircominternational.com/Schemas/Connect/2010/08" xmlns:ct="http://www.aircominternational.com/Schemas/CommonTypes/2009/05" xmlns:co="http://www.aircominternational.com/Schemas/Common/2009/07" xmlns:gsm80="http://www.aircominternational.com/Schemas/GSM/2011/04" xmlns:ecs="http://www.aircominternational.com/Schemas/EWS/ECSCoverageTypes/2011/08" xmlns:gsm70="http://www.aircominternational.com/Schemas/GSM/2010/08" xmlns:config="http://www.aircominternational.com/Schemas/Configuration/2010/08" xmlns:cdma="http://www.aircominternational.com/Schemas/CDMA/2010/12" xmlns:co70="http://www.aircominternational.com/Schemas/Common/2010/08" xmlns:umts80="http://www.aircominternational.com/Schemas/UMTS/2011/04" xmlns:eqp70="http://www.aircominternational.com/Schemas/Equipment/2010/08" xmlns:co80="http://www.aircominternational.com/Schemas/Common/2011/04" xmlns:util="http://www.aircominternational.com/contract/Util/2009/10" xmlns:tra80="http://www.aircominternational.com/Schemas/Connect/2011/04" xmlns:umts70="http://www.aircominternational.com/Schemas/UMTS/2010/08" xmlns:eqp80="http://www.aircominternational.com/Schemas/Equipment/2011/04" xmlns:tra="http://www.aircominternational.com/Schemas/Connect/2009/09" xmlns:lte="http://www.aircominternational.com/Schemas/LTE/2010/08" xmlns:eds="http://www.aircominternational.com/contract/EDS/2009/05" xmlns:umts2="http://www.aircominternational.com/Schemas/UMTS/2009/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:eqp="http://www.aircominternational.com/Schemas/Equipment/2009/09" xmlns:gsm="http://www.aircominternational.com/Schemas/GSM/2009/09" xmlns:lte80="http://www.aircominternational.com/Schemas/LTE/2011/04"> 
    <RootEntityType xsi:type="gsm:MSCType" ct:iid="OrphanedBSCs" ct:bvid="IVLP_201303" ct:eid="223127"> 
    <Security> 
     <CreateDate>2013-03-21T10:01:18</CreateDate> 
     <ModifyDate>2013-03-21T10:01:19</ModifyDate> 
     <CreateUser>EDS_SOA_Client</CreateUser> 
     <ModifyUser>EDS_SOA_Client</ModifyUser> 
     <UserGroup>All</UserGroup> 
     <Permissions> 
     <Owner>write</Owner> 
     <Group>write</Group> 
     <All>read</All> 
     </Permissions> 
    </Security> 
    <CustomFields> 
     <Field Group="SOURCE" Value="NETWORK"/> 
     <Field Group="LastUpdated" Value="3/21/2013 10:01:18 AM"/> 
    </CustomFields> 
    <Location ct:iid="OrphanedBSCs_msc_loc" ct:eid="119692"/> 
    <gsm:BSCs> 
     <gsm:BSC ct:iid="OrphanedSites" ct:bvid="IVLP_201303" ct:eid="223128"> 
     <Security> 
      <CreateDate>2013-03-21T10:01:19</CreateDate> 
      <ModifyDate>2013-03-21T10:01:19</ModifyDate> 
      <CreateUser>EDS_SOA_Client</CreateUser> 
      <ModifyUser>EDS_SOA_Client</ModifyUser> 
      <UserGroup>All</UserGroup> 
      <Permissions> 
      <Owner>write</Owner> 
      <Group>write</Group> 
      <All>read</All> 
      </Permissions> 
     </Security> 
     <CustomFields> 
      <Field Group="SOURCE" Value="NETWORK"/> 
      <Field Group="LastUpdated" Value="3/21/2013 10:01:19 AM"/> 
     </CustomFields> 
     <Location ct:iid="OrphanedSites_bsc_loc" ct:eid="119693"/> 
     <gsm:Parent ct:iid="OrphanedBSCs" ct:eid="223127"/> 
     <gsm:BSCId>0</gsm:BSCId> 
     <gsm:AllowedOperations>ReadWrite</gsm:AllowedOperations> 
     </gsm:BSC> 
    </gsm:BSCs> 
    <gsm:AllowedOperations>ReadWrite</gsm:AllowedOperations> 
    </RootEntityType> 
    <RootEntityType xsi:type="gsm:MSCType" ct:iid="HUAWEI_2G_MSC" ct:bvid="IVLP_201303" ct:eid="223131"> 
    <Security> 
     <CreateDate>2013-03-21T10:01:21</CreateDate> 
     <ModifyDate>2013-03-21T10:01:21</ModifyDate> 
     <CreateUser>EDS_SOA_Client</CreateUser> 
     <ModifyUser>EDS_SOA_Client</ModifyUser> 
     <UserGroup>All</UserGroup> 
     <Permissions> 
     <Owner>write</Owner> 
     <Group>write</Group> 
     <All>read</All> 
     </Permissions> 
    </Security> 
    <CustomFields> 
     <Field Group="SOURCE" Value="NETWORK"/> 
     <Field Group="LastUpdated" Value="3/21/2013 10:01:21 AM"/> 
    </CustomFields> 
    <Location ct:iid="HUAWEI_2G_MSC_msc_loc" ct:eid="119696"/> 
    <gsm:BSCs> 
     <gsm:BSC ct:iid="BSCMG35" ct:bvid="IVLP_201303" ct:eid="223252"> 
     <Security> 
      <CreateDate>2013-03-21T10:01:30</CreateDate> 
      <ModifyDate>2013-03-21T10:01:30</ModifyDate> 
      <CreateUser>EDS_SOA_Client</CreateUser> 
      <ModifyUser>EDS_SOA_Client</ModifyUser> 
      <UserGroup>All</UserGroup> 
      <Permissions> 
      <Owner>write</Owner> 
      <Group>write</Group> 
      <All>read</All> 
      </Permissions> 
     </Security> 
     <CustomFields> 
      <Field Group="SOURCE" Value="NETWORK"/> 
      <Field Group="LastUpdated" Value="3/21/2013 10:01:22 AM"/> 
     </CustomFields> 
     <Location ct:iid="BSCMG35_bsc_loc" ct:eid="119817"/> 
     <gsm:Parent ct:iid="HUAWEI_2G_MSC" ct:eid="223131"/> 
     <gsm:BSCId>0</gsm:BSCId> 
     <gsm:AllowedOperations>ReadWrite</gsm:AllowedOperations> 
     </gsm:BSC> 
     <gsm:BSC ct:iid="BSCMG34" ct:bvid="IVLP_201303" ct:eid="223270"> 
     <Security> 
      <CreateDate>2013-03-21T10:01:30</CreateDate> 
      <ModifyDate>2013-03-21T10:01:30</ModifyDate> 
      <CreateUser>EDS_SOA_Client</CreateUser> 
      <ModifyUser>EDS_SOA_Client</ModifyUser> 
      <UserGroup>All</UserGroup> 
      <Permissions> 
      <Owner>write</Owner> 
      <Group>write</Group> 
      <All>read</All> 
      </Permissions> 
     </Security> 
     <CustomFields> 
      <Field Group="SOURCE" Value="NETWORK"/> 
      <Field Group="LastUpdated" Value="3/21/2013 10:01:22 AM"/> 
     </CustomFields> 
     <Location ct:iid="BSCMG34_bsc_loc" ct:eid="119835"/> 
     <gsm:Parent ct:iid="HUAWEI_2G_MSC" ct:eid="223131"/> 
     <gsm:BSCId>0</gsm:BSCId> 
     <gsm:AllowedOperations>ReadWrite</gsm:AllowedOperations> 
     </gsm:BSC> 
    </gsm:BSCs> 
    <gsm:AllowedOperations>ReadWrite</gsm:AllowedOperations> 
    </RootEntityType> 
</ArrayOfRootEntityType> 
+2

有幾種方法可以實現這一點,你可以在XML數據上創建一個視圖,或者創建一個包含所需列的表格,然後僅使用所需元素進行插入/選擇。我強烈建議您閱讀Oracle Docs,因爲它們相當不錯 - http://docs.oracle.com/cd/E11882_01/appdev.112/e16659/toc.htm – OldProgrammer

回答

1

使用這樣的事情:

select * 
from table-with-xml-column t 
,  xmltable 
     ('PATH/TO/WHAT/YOU/NEED' 
     passing t.xml_column_name 
     columns column1 number  path 'path1' 
     columns column2 char(1 byte) path 'path2' 
     columns column3 varchar2(30) path 'path3' 
     ) t2 

提示!請注意,處理XML列可能會非常緩慢,Oracle似乎經常分析xmltype的內容。不要在XML和關係數據或高頻執行的語句之間引入複雜的連接,而不會測試具有實際大小的XML內容的性能。

+0

嗨,感謝您的幫助,此示例爲非常有用,但在一些標籤中,我無法使用它。例如,在這個標籤「 」我無法獲得值「BSCMG35」的消息錯誤ORA- 19228:XPST0008 - 未聲明的標識符:前綴'gsm'本地名稱'gsm:BSC'被顯示。 我使用查詢:SELECT 的b * FROM MSC_XML P, 的XMLTable( '/ ArrayOfRootEntityType/RootEntityType/GSM:的BSC/GSM:BSC' PASSING p.OBJECT_VALUE COLUMNS MSC VARCHAR2(38)PATH「@ *: iid')ab; – user3169822

+0

啊好的。請記住檢查命名空間的使用情況。沒有它,無論是出錯還是無法訪問:-)。例如,像這樣:select extractValue(l_xml,'/ ipinfo/ip_address',g_ns) –

0

感謝您的幫助。 我解決了我的問題,設置XML文件中使用的名稱空間。 這是新的查詢以xml選擇任何數據:

SELECT * 從表用的XML列噸 ,XMLTABLE ( (XMLNAMESPACES( 'url_indicated_in_xmlfile_about_ct' 爲 「CT」), 「PATH/TO/WHAT/YOU/NEED」 傳遞t.xml_column_name 第COLUMN1數路徑 'PATH1' 列COLUMN2炭(1個字節)路徑 'PATH2' 列欄3 VARCHAR2(30)路徑 'path3時,' )T2

+0

好的,很高興聽到你受到了幫助。我認爲這個補充的一個好地方是使用'編輯'將它放在問題本身中。審查人員可能會將其移至此處。 –