Reading XML Data
Reading XML Data
This Document explains the steps of how we read an XML data file using PLSQL and write them to
oracle tables.
Creating a Logical directory and giving the Permissions to access this directory using an URL.
Using the XML Dom Parser Procedures to read the Xml file , parse it
and then load it into the respective columns.
==>
/apps/applmgr/tst1ora/IAS/Apache/Apache/conf
Page 1 of 1
Create a procedure to parse and retrieve the XML data and then insert it into appropriate
columns in the database.
1. XMLParser –
Procedures
i.parse(p Parser, url VARCHAR2)
Description – The parse procedure takes two parameters which are the parse object and the url of the
xml file that has to parsed.
iv getDocument(p Parser)
Get the Document which has to be parsed.
2. XMLDOM
DOMDocument
DOMELEMENT
DOMNODELIST
DOMNODE
DOMNamedNodeMap
FUNCTIONS –
getNodeName(n DOMNode) RETURN VARCHAR2 - Retrieves the Name of the Node
getFirstChild(n DOMNode) RETURN DOMNode - Retrieves the first child of the node
getLength(nl DOMNodeList) RETURN NUMBER- Retrieves the number of items in the list.
Page 2 of 2
getLength(nnm DOMNamedNodeMap) RETURN NUMBER - Retrieves the number of items in the
map.
item(nl DOMNodeList, idx IN NUMBER) RETURN DOMNode - Retrieves the item given the index
in the nodelist
item(nnm DOMNamedNodeMap, idx IN NUMBER) RETURN DOMNode - Retrieves the item given
the index in the map
Page 3 of 3
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="BRL" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="CAD" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="CNY" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="DKK" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="USD" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="HKD" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="3" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="INR" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="2" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="JPY" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="MYR" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="MXN" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="USD" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="NOK" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="SGD" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="ZAR" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="2" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="KRW" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="3" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="LKR" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="SEK" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
Page 4 of 4
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="CHF" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="3" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="TWD" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="3" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="THB" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="USD" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
+ <frbny:Series AVAILABILITY="A" DECIMALS="2" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="VEB" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
</frbny:DataSet>
</UtilityData>
Page 5 of 5
tp varchar2(100);
rate varchar2(100);
begin
-- get all elements
nl1 := xmldom.getElementsByTagName(doc,'Series');
nl2 := xmldom.getElementsByTagName(doc,'CURR');
nl3 := xmldom.getElementsByTagName(doc,'TIME_PERIOD');
nl4 := xmldom.getElementsByTagName(doc,'OBS_VALUE');
len1 := xmldom.getLength(nl1);
len2 := xmldom.getLength(nl2);
len3 := xmldom.getLength(nl3);
len4 := xmldom.getLength(nl4);
-- loop through elements
for i in 0..len1-1 loop
n1 := xmldom.item(nl1,i);
n2 := xmldom.item(nl2,i);
n3 := xmldom.item(nl3,i);
n4 := xmldom.item(nl4,i);
dbms_output.put(xmldom.getNodeName(n2));
dbms_output.put(xmldom.getNodeName(n3));
dbms_output.put(xmldom.getNodeName(n4));
n2 := xmldom.getFirstChild(n2);
n3 := xmldom.getFirstChild(n3);
n4 := xmldom.getFirstChild(n4);
--if xmldom.getNodeType(n1) = xmldom.TEXT_NODE then
dbms_output.put_line('=' || xmldom.getNodeValue(n2));
--if xmldom.getNodeType(n2) = xmldom.TEXT_NODE then
dbms_output.put_line('=' || xmldom.getNodeValue(n3));
dbms_output.put_line('=' || xmldom.getNodeValue(n4));
--end if;
--end if;
from_cur:=xmldom.getNodeValue(n2);
tp:=xmldom.getNodeValue(n3);
rate:=xmldom.getNodeValue(n4);
-- loop through attributes
nnm := xmldom.getAttributes(n1);
if (xmldom.isNull(nnm) = FALSE) then
len5 := xmldom.getLength(nnm);
for j in 0..len5-1 loop
n1 := xmldom.item(nnm, j);
attrname := xmldom.getNodeName(n1);
attrval := xmldom.getNodeValue(n1);
if(attrname='UNIT') then
dbms_output.put(' ' || attrname || ' = ' || attrval);
to_cur:=attrval;
Page 6 of 6
end if;
end loop;
dbms_output.put_line('');
end if;
insert into kaz_testing_xml_file_stg
(ls_from_currency,ls_to_currency,ls_from_conv_date,ls_to_conv_date,LS_USR_CONV_TYPE,ls_co
nv_rate_base) values (from_cur,to_cur,TO_DATE(tp,'YYYY-MM-DD'),TO_DATE(tp,'YYYY-MM-
DD'),'Spot',to_number(rate));
dbms_output.put_line(' ');
end loop;
dbms_output.put_line('');
end printElements;
begin
-- new parser
p := xmlparser.newParser;
-- set some characteristics
xmlparser.setValidationMode(p, FALSE);
--xmlparser.setErrorLog(p, dir || '/' || errfile);
xmlparser.setBaseDir(p, dir);
-- parse input file
xmlparser.parse(p, dir || '/' || inpfile);
-- get document
doc := xmlparser.getDocument(p);
-- Print document elements
dbms_output.put('The elements are: ');
printElements(doc);
end;
7) Execute the procedure to insert the xml data from into the database as follows:
Exec domsample3(‘http://sharp.apps.com:8001/attachment’,’test.xml’);
Page 7 of 7