oracle xml handling

Post on 18-Jul-2015

450 Views

Category:

Education

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

XML Storage Options:1. VARCHAR2 – unstructured, limited size2. CLOBs – unstructured, max file = 2GB3. XMLType – structured, associate with XDK and other XML

operations

XML DB Architecture:1. XML DB Repository2. DOM fidelity3. SQL* Loader

Using XMLTYPE

XML Piecewise UpdateUpdate part of the xml document in the database specified by the XPath expression.

XML Schema ValidationManually or automatically validate XML documents as they are inserted to the Database.

XML Document GenerationGenerate XML data from database using normal SQL query.

Creating XMLType Column or table with optional XML Schema support

create table profile(pid number,pfile XMLType);

create table profile of XMLType;

create table profile of XMLTypeXMLSCHEMA “http://bumbus.ucdavis.edu/scholar.xsd”ELEMENT “UCLEADS”

Declares XMLType Column

Declares XMLType Table

Not Recommended

Declares XMLType Table conformed to an XML Schema and specific the root element of the xml document to be inserted.

Storing XML document into the databaseinsert into profilevalues(100, XMLType('

<ScholarProfile><ID>1</ID><LastName> Azzzr</LastName><FirstName>Hussain</FirstName><Email>fdsfsafafa@mail.com</Email><Major>Load Runner</Major><Grade>A</Grade>

</ScholarProfile>‘ ));

Insert the whole XML document in SQL query

Accessing XML data stored as XMLType instance

1. ExtractValue()- access the value of an XML node

2. ExistsNode() - check if a particular node existed

3. Exact() - extract a collection of XML nodes

4. XMLSequence()- converts a fragment of XML into a collection (a table) of XMLType instances.

SELECT extract(X.pfile,'UCLEADS/ScholarProfile/Major')FROM profile X;

Results are returned as a fragment of XML

SELECT extractValue(value(w),'/Major') FROM profile X, TABLE ( xmlsequence (

extract(value(X), '/UCLEADS/ScholarProfile/Major'))) w;

Values are extracted from the nodes of the XMLType table generated using the XMLSequence()

SELECT existsNode(x.pfile, '/UCLEADS/ScholarProfile/Major') SubFound, existsNode(x.pfile, '/UCLEADS/ScholarProfile[Major="ORACLE"]') MajorFound, existsNode(x.pfile, '/UCLEADS/ScholarProfile[Major="Oracle"]') MajorFound2 FROM Profile1 X;

SELECT count(*) FROM Profile X WHERE existsNode(x.pfile, '/UCLEADS/ScholarProfile[Major="Oracle"‘]) = 1;

<EMPLOYEES><EMP>

<EMPNO>112</EMPNO><EMPNAME>Joe</EMPNAME><SALARY>50000</SALARY>

</EMP><EMP>

<EMPNO>217</EMPNO><EMPNAME>Jane</EMPNAME><SALARY>60000</SALARY>

</EMP><EMP>

<EMPNO>412</EMPNO><EMPNAME>Jack</EMPNAME><SALARY>40000</SALARY>

</EMP></EMPLOYEES>

SELECT UPDATEXML(emp_col, '/EMPLOYEES/EMP[EMPNAME="Joe"]/SALARY/text()', 100000,

'//EMP[EMPNAME="Jack"]/EMPNAME/text()','Jackson',

'//EMP[EMPNO=217]', XMLTYPE.CREATEXML('<EMP><EMPNO>217</EMPNO><EMPNAME>Jane</EMPNAME>'))

FROM emp_tab e;

UPDATEXML takes as arguments an XMLType instance and an XPath-value pair and returns an XMLType instance with the updated value

<EMPLOYEES><EMP><EMPNO>112</EMPNO><EMPNAME>Joe</EMPNAME><SALARY>100000</SALARY></EMP><EMP><EMPNO>217</EMPNO><EMPNAME>Jane</EMPNAME></EMP><EMP><EMPNO>412</EMPNO><EMPNAME>Jackson</EMPNAME><SALARY>40000</SALARY></EMP></EMPLOYEES>

<EMPLOYEES><EMP>

<EMPNO>112</EMPNO><EMPNAME>Joe</EMPNAME><SALARY>50000</SALARY>

</EMP><EMP>

<EMPNO>217</EMPNO><EMPNAME>Jane</EMPNAME><SALARY>60000</SALARY>

</EMP><EMP>

<EMPNO>412</EMPNO><EMPNAME>Jack</EMPNAME><SALARY>40000</SALARY>

</EMP></EMPLOYEES>

CREATE VIEW new_emp_viewAS SELECT

UPDATEXML(emp_col, '/EMPLOYEES/EMP/SALARY/text()', 0) emp_view_col

FROM emp_tab e

UPDATE profile t SET value(t) = updateXML(value(t),'/UCLEADS/ScholarProfile/Major/text()','CS') WHERE existsNode(value(t),

'/UCLEADS/ScholarProfile[Major="Computer Science"]') = 1;

XML Piecewise Update

• isFragment() – returns (1) if the XMLType contains XML document fragment.

• getClobVal() – converts the XMLType document into CLOB object.

• getRootElement() – get the root element of the XML document.

• getNameSpace() – get the namespace of the root element of the XML document.

select xmltype(xmltype.getclobVal(t.pfile)).getRootElement() Exmpl1, xmltype.getRootElement(t.pfile)Exmp2, xmltype.isFragment(t.pfile)isFrag1 , xmltype.isFragment(extract(t.pfile,'/UCLEADS/ScholarProfile'))isFrag2 from profile1 t;

azharpro@gmail.com

top related