Вы находитесь на странице: 1из 7

Reading XML Data Using PLSQL

This Document explains the steps of how we read an XML data file using PLSQL and write them to
oracle tables.

This involves the following steps.

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.

1) Creation of a URL at the Server

Creation of a URL at the Server

1) Create a logical directory

Create or replace directory TEST_LOB_DIR as '/data/tst1/attachment'

It creates a directory with Owner SYS.

Grant privileges on this directory to Apps.

2) Create a physical directory at the given location ‘/data/tst1/attachment’

Permissions on this directory too.

3) Edit the conf file at this path


Cd $IAS_ORACLE_HOME/Apache/Apache/conf

==>
/apps/applmgr/tst1ora/IAS/Apache/Apache/conf

Edit the file apps.conf

Alias /attachment/ "/data/tst1/attachment/"


<Location /attachment/>
Order allow, deny
Allow from all
</Location>

4) Bounce the Apache.

Page 1 of 1
Create a procedure to parse and retrieve the XML data and then insert it into appropriate
columns in the database.

Used two PLSQL packages for these – 1. XMLPARSER


2. XMLDOM

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.

ii. setValidationMode(p Parser, yes BOOLEAN)


Checks whether XML is Valid or not.

iii. setBaseDir(p Parser, dir VARCHAR2)


Sets the base url or directory path.

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

getNodeValue(n DOMNode) RETURN VARCHAR2 - Retrieves the Value of the Node

getElementsByTagName(doc DOMDocument, tagname IN VARCHAR2)- Retrieves the elements in


the by tag name

getDocumentElement(doc DOMDocument) RETURN DOMElement - Retrieves the root element of


the document

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

getAttributes(n DOMNode) RETURN DOMNamedNodeMap - Retrieves the attributes of the node

XML Data Source (test.xml) :-

<?xml version="1.0" encoding="UTF-8" ?>


- <UtilityData xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/message"
xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/common"
xmlns:compact="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/compact"
xmlns:cross="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/cross"
xmlns:generic="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/generic"
xmlns:query="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/query"
xmlns:structure="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/structure"
xmlns:utility="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/utility"
xmlns:frbny="http://www.newyorkfed.org/xml/schemas/FX/utility"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/messa
ge http://www.sdmx.org/data/SDMXMessage.xsd
http://www.newyorkfed.org/xml/schemas/FX/utility
http://www.newyorkfed.org/xml/schemas/FX_Utility.xsd">
- <Header>
<ID>FXOneDay</ID>
<Test>false</Test>
<Name xml:lang="en">Foreign Exchange Noon Rates</Name>
<Prepared>2006-07-06</Prepared>
+ <Sender id="FRBNY">
</Header>
- <frbny:DataSet>
- <frbny:Series AVAILABILITY="A" DECIMALS="4" UNIT_MULT="0"
TIME_FORMAT="P1D" UNIT="USD" FX_METHOD="12" DISCLAIMER="12"
AUTHORITY="12">
- <frbny:Key>
<frbny:FREQ>D</frbny:FREQ>
<frbny:CURR>AUD</frbny:CURR>
<frbny:FX_TIME>12</frbny:FX_TIME>
<frbny:FX_TYPE>S</frbny:FX_TYPE>
</frbny:Key>
- <frbny:Obs OBS_STATUS="A" OBS_CONF="F">
<frbny:TIME_PERIOD>2006-07-05</frbny:TIME_PERIOD>
<frbny:OBS_VALUE>0.7407</frbny:OBS_VALUE>
</frbny:Obs>
</frbny:Series>

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>

Procedure to insert XML data in database using DOM PL/SQL API -

create or replace procedure domsample3(dir varchar2, inpfile varchar2)


is
p xmlparser.parser;
doc xmldom.DOMDocument;
docelem dbms_xmldom.DOMElement;

-- prints elements in a document


procedure printElements(doc xmldom.DOMDocument) is
nl1 xmldom.DOMNodeList;
nl2 xmldom.DOMNodeList;
nl3 xmldom.DOMNodeList;
nl4 xmldom.DOMNodeList;
len1 number;
len2 number;
len3 number;
len4 number;
len5 number;
n1 xmldom.DOMNode;
n2 xmldom.DOMNode;
n3 xmldom.DOMNode;
n4 xmldom.DOMNode;
nnm xmldom.DOMNamedNodeMap;
attrname varchar(1000);
attrval varchar(1000);
from_cur varchar2(15);
to_cur varchar2(15);

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;

Compile the procedure domsample3.

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

Вам также может понравиться