How to read tabs and labels in an XML file from SQL and PL/SQL?
867895Oct 3 2011 — edited Oct 4 2011I have some steps in Oracle to select the contents of an xml file, that work fine if all the tags and labels in the xml file match what I am looking for.
My problem is that sometimes someone will change one of the tags/labels in an xml file and not tell me. If that has happened, my select statement
does not return an error. If the XMLTABLE('/tblCageDuns/CageDuns' portion does not match the xml file (ex: instead of CageDuns, the xml file may
have CageDun), I get no rows returned. If one of the "columns" does not match (ex: instead of 'cage' the xml file may have 'cages'), I get a null
value for the column. Is there a way to read what all the tags and labels are in the xml file, so I can check them before I try to query the data?
Here are my basic steps:
1. Create a table with an XML column:
create table cagexml_col (
xmlfile_id number primary key,
xmlfile_doc XMLType);
2. Insert xml document:
insert into cagexml_col values (1,
XMLType(bfilename('ING_FEED', 'CAGEDUNS.xml'),
nls_charset_id('AL32UTF8')));
3. Read data:
select xmlfile_id,
a.CAGE,
a.DUNS,
a.SRCFLAG
FROM cagexml_col,
XMLTABLE('/tblCageDuns/CageDuns'
PASSING cagexml_col.xmlfile_doc
COLUMNS
CAGE VARCHAR2(5) Path 'cage',
DUNS VARCHAR2(9) Path 'duns',
SRCFLAG number Path 'SrcFlag'
) a
where xmlfile_id=1;
Here is a sample file CAGEDUNS.xml.
<tblCageDuns><CageDuns><cage>AAA87</cage><duns>111149236</duns><SrcFlag>0</SrcFlag></CageDuns><CageDuns><cage>BBB95</cage><duns>222245890</duns><SrcFlag>4099</SrcFlag></CageDuns><CageDuns><cage>CCC90</cage><duns>333368885</duns><SrcFlag>3</SrcFlag></CageDuns><CageDuns><cage>DDD14</cage><duns>444437379</duns><SrcFlag>3</SrcFlag></CageDuns><CageDuns><cage>EEE77</cage><duns>555597063</duns><SrcFlag>4097</SrcFlag></CageDuns><CageDuns><cage>FFF56</cage><duns>666639159</duns><SrcFlag>3</SrcFlag></CageDuns><CageDuns><cage>GGG95</cage><duns>777745890</duns><SrcFlag>4097</SrcFlag></CageDuns><CageDuns><cage>HHH16</cage><duns>888882310</duns><SrcFlag>3</SrcFlag></CageDuns><CageDuns><cage>III50</cage><duns>999987789</duns><SrcFlag>3</SrcFlag></CageDuns><CageDuns><cage>JJJ69</cage><duns>000073027</duns><SrcFlag>3</SrcFlag></CageDuns><CageDuns><cage>KKK77</cage><duns>121297063</duns><SrcFlag>4099</SrcFlag></CageDuns><CageDuns><cage>LLL68</cage><duns>232300000</duns><SrcFlag>3</SrcFlag></CageDuns></tblCageDuns>
Here is the version info:
**********************************************************************************
SQL*Plus: Release 10.2.0.1.0 - Production on Mon Oct 3 16:30:40 2011
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
MAX_INTF@romsprd SQL>select * from V$VERSION
2 /
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
**********************************************************************************