使用Oracle读取xml
我们必须使用pl/sql读取xml。xml的前几行粘贴在下面。在xml中,对于一个节点,有一个设备。对于一台设备,有多个机柜。对于一个机柜,有多个机架;对于一个机架,有多个板 我们开发了一个下面的查询来解析 步骤1:使用Oracle读取xml,oracle,Oracle,我们必须使用pl/sql读取xml。xml的前几行粘贴在下面。在xml中,对于一个节点,有一个设备。对于一台设备,有多个机柜。对于一个机柜,有多个机架;对于一个机架,有多个板 我们开发了一个下面的查询来解析 步骤1: create table emp_xml of xmltype xmltype store as securefile binary xml; 步骤2: insert into emp_xml values (xmltype(bfilename('XML_DIR','ahm_20
create table emp_xml of xmltype xmltype store as securefile binary xml;
步骤2:
insert into emp_xml values (xmltype(bfilename('XML_DIR','ahm_2015_04_01_172428.xml'), nls_charset_id('AL32UTF8') ));
步骤3:
select * from emp_xml;
步骤4:
select x.*
from emp_xml t,
xmltable(xmlnamespaces(default 'http://www.ericsson.com/axe/export/hw'(http :/ /
www.ericsson.com / axe /
export /
hw%27)),
'/NetworkInventory/Node' passing t.object_value columns
SiteName varchar2(10) path '@Name',
SiteType varchar2(10) path '@Type',
BuildingPractice varchar2(10) path
'//Equipment/@BuildingPractice') x;
这个查询运行得很好。但是当我试图获取机柜或子架的详细信息时,我们得到了以下错误
ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence
ORA-06512: at line 33
19279. 00000 - "XQuery dynamic type mismatch: expected singleton sequence - got multi- item sequence"
*Cause: The XQuery sequence passed in had more than one item.
*Action: Correct the XQuery expression to return a single item sequence.
下面给出了XML的前几行。
AXE硬件清单数据
因为您的文件柜不是一次迭代设置的。如果要以关系格式显示重复组,则必须提取主XQuery表达式中的项目序列。
然后将每个项传递给COLUMNS子句,以便进一步分解为列。您正在尝试展开一个类似于嵌套表的构造。您的设备节点可以有多个机柜,因此要从这些机柜中提取详细信息,您需要将其传递到第二个XMLTable:
select x.SiteName, x.SiteType, x.BuildingPractice, y.Position
from emp_xml t
cross join xmltable(
xmlnamespaces(default 'http://www.ericsson.com/axe/export/hw'),
'/NetworkInventory/Node' passing t.object_value columns
SiteName varchar2(10) path '@Name',
SiteType varchar2(10) path '@Type',
BuildingPractice varchar2(10) path 'Equipment/@BuildingPractice',
Equipment XMLType path 'Equipment'
) x
cross join xmltable(
xmlnamespaces(default 'http://www.ericsson.com/axe/export/hw'),
'//Cabinet' passing x.Equipment columns
Position varchar2(15) path '@Position'
) y;
SITENAME SITETYPE BUILDINGPRACTICE POSITION
---------- ---------- ---------------- ---------------
BSC20 AXE BYB501 CabNumber=1
要获取子框数据,还需要将其传递给第三级XMLTable,等等
select x.SiteName, x.SiteType, x.BuildingPractice, y.Position
from emp_xml t
cross join xmltable(
xmlnamespaces(default 'http://www.ericsson.com/axe/export/hw'),
'/NetworkInventory/Node' passing t.object_value columns
SiteName varchar2(10) path '@Name',
SiteType varchar2(10) path '@Type',
BuildingPractice varchar2(10) path 'Equipment/@BuildingPractice',
Equipment XMLType path 'Equipment'
) x
cross join xmltable(
xmlnamespaces(default 'http://www.ericsson.com/axe/export/hw'),
'//Cabinet' passing x.Equipment columns
Position varchar2(15) path '@Position'
) y;
SITENAME SITETYPE BUILDINGPRACTICE POSITION
---------- ---------- ---------------- ---------------
BSC20 AXE BYB501 CabNumber=1