Download presentation
Presentation is loading. Please wait.
Published byIsaac Johns Modified over 8 years ago
1
11 Copyright © 2004, Oracle. All rights reserved. Managing XML Data in an Oracle 10g Database
2
11-2 Copyright © 2004, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Describe XMLType Create objects of XMLType Describe XMLType storage options Load data into XMLType Retrieve data from XMLType columns Use XMLType member functions called methods Use XPath functions on XMLType data
3
11-3 Copyright © 2004, Oracle. All rights reserved. XMLType XMLType : Is a data type in Oracle database10g Stores XML data or documents in a native database format by using: –A CLOB –Object-relational tables and views Can be accessed by all Oracle SQL interfaces, bringing the XML and SQL worlds together Enables SQL operations on XML content, and XML operations on SQL content Provides built-in functions for indexing, and navigation of XML content
4
11-4 Copyright © 2004, Oracle. All rights reserved. The Benefits of Using XMLType XMLType : Is a data type for database columns, object type attributes, object tables, and views Provides constructors to create XMLType instances for insertion Can be queried with SQL, using built-in member functions on the XML content Can be used in the PL/SQL variable declarations, subprogram parameters, and function return values Supports references to registered XML Schemas Can be indexed by using Oracle Text indexes
5
11-5 Copyright © 2004, Oracle. All rights reserved. Declaring an XMLType For a column in a relational table: For an object-relational table: For an attribute in an object type: CREATE TABLE emp_resumes ( employee_id NUMBER(6) PRIMARY KEY, resume XMLType); CREATE TABLE emp_xmlresumes OF XMLType; CREATE TYPE resume_t AS OBJECT ( employee_id NUMBER(6), resume XMLType); /
6
11-6 Copyright © 2004, Oracle. All rights reserved. XMLType Storage Characteristics The XMLType is a virtual column: Encapsulating a real CLOB column type That allows the characteristics for the CLOB storage to be specified with a STORE AS clause CREATE TABLE emp_resumes_clob ( employee_id NUMBER(6) PRIMARY KEY, resume XMLType) XMLType COLUMN resume STORE AS CLOB (TABLESPACE data01 STORAGE (INITIAL 4096 NEXT 4096) CHUNK 4096 NOCACHE LOGGING);
7
11-7 Copyright © 2004, Oracle. All rights reserved. Unstructured Versus Structured Storage Employees table... XMLType Column......... Steven King CLOB......... CLOB...... Create XMLType table Structured storage Store as CLOB The XML document or fragments are stored in a CLOB data type. The XML elements are mapped to columns in tables. Employees tables first_name last_name email … dept_id Steven King SKING … 90 --------- ----------- ------- --------- employees.xml employees.xsd
8
11-8 Copyright © 2004, Oracle. All rights reserved. CLOB Versus Object-Relational Storage CLOB StorageObject-Relational Storage Uses text searchUses full SQL search Uses Oracle Text indexUses Oracle Text and B-Tree index Is easier to generateIs more complex to generate Is flexible for Schema changesOffers limited flexibility Maintains document fidelityMaintains DOM fidelity Supports whole document updates Supports piecewise updates Provides better performance for storage and retrieval Provides better performance for DML operations Uses more space because of tags and spaces Uses less space because only data, and not metadata, is stored
9
11-9 Copyright © 2004, Oracle. All rights reserved. Loading Data into XMLType You can load data into XMLType columns by using: An INSERT with the XMLType() constructors by using a VARCHAR2, CLOB, BLOB, or a BFILE : Utilities, such as: –Oracle XML/SQL Utility for Java, FTP, and WebDAV –Oracle export, import, and SQL*Loader INSERT INTO emp_resumes VALUES (100, XMLType( ' Steven King AD_PRES '));
10
11-10 Copyright © 2004, Oracle. All rights reserved. Overview of Querying XML Select XMLType columns: Use XMLType member functions: – existsNode() to filter query results based on the XML content – extract() to extract XML fragments or nodes – extractValue() to extract text node values Query the XML content by using Oracle Text operators SELECT resume FROM emp_resumes WHERE employee_id = 100;
11
11-11 Copyright © 2004, Oracle. All rights reserved. Subset of XMLType Member Functions XMLType Function Description getClobVal() Returns the XMLType contents as a CLOB value getStringVal() Returns the XMLType value as a string value getNumberVal() Returns the XMLType mode as a numeric value isFragment() Returns the value 1 if the document is a fragment; otherwise, returns 0 (when the XML declaration is present) SELECT e.resume.getClobVal() FROM emp_resumes e WHERE employee_id = 100;
12
11-12 Copyright © 2004, Oracle. All rights reserved. Querying an XMLType by Using XPath The SQL and XMLType functions that use XPath expressions are: existsNode() extract() extractValue() SQL function: existsNode(xmltype, xpath-expr) XMLType function: xmltype.existsNode(xpath-expr) SQL function: extract(xmltype, xpath-expr) XMLType function: xmltype.extract(xpath-expr) SQL function: extractValue(xmltype,xpath-expr) XMLType function: xmltype.extractValue(xpath-expr)
13
11-13 Copyright © 2004, Oracle. All rights reserved. Using the existsNode() Function As a SQL function: As an XMLType member function: In the SELECT list: SELECT COUNT(*) FROM emp_resumes WHERE existsNode(resume, '/RESUME[FULL_NAME="Steven King"]') = 1; SELECT employee_id FROM emp_resumes e WHERE e.resume.existsNode( '//JOB_ID[text()="AD_VP"]') = 1; SELECT employee_id, existsNode(resume, '//JOB_ID[text()="AD_VP"]') VP FROM emp_resumes;
14
11-14 Copyright © 2004, Oracle. All rights reserved. Using the extract() Function The extract() function: Accepts an XPath expression parameter Returns an XMLType instance Can be used for pretty printing of XML results Example: Result: SELECT e.resume.extract('//JOB_ID/text()') result FROM emp_resumes e WHERE e.employee_id = 100; RESULT ----------------- AD_PRES
15
11-15 Copyright © 2004, Oracle. All rights reserved. Using the extract() Function Extracting a fragment: Testing for fragments: SELECT employee_id, e.resume.extract('//FULL_NAME') result FROM emp_resumes e; EMPLOYEE_ID RESULT ----------- ------------------------------------- 100 Steven King 101 Neena Kochhar 102 Lex De Haan SELECT extract(resume,'//JOB_HISTORY') result, extract(resume,'//JOB_HISTORY').isFragment() frag FROM emp_resumes WHERE employee_id = 101;
16
11-16 Copyright © 2004, Oracle. All rights reserved. Combining extract() and existsNode() It is typical to combine extract() and existsNodes() in the same query. Example: Results: SELECT employee_id, extract(resume, '//JOB_ID/text()') result FROM emp_resumes WHERE existsNode(resume, '/RESUME[FULL_NAME="Steven King"]') = 1; EMPLOYEE_ID RESULT ----------- -------------------- 100 AD_PRES
17
11-17 Copyright © 2004, Oracle. All rights reserved. Using the extractValue() Function Extracting the text value of a “single” node does not require using the text() XPath function: In comparison, using extract() requires using the text() XPath function: SELECT employee_id, extractValue(resume, '//FULL_NAME') result FROM emp_resumes; EMPLOYEE_ID RESULT ----------- -------------------- 100 Steven King 101 Neena Kochhar 102 Lex De Haan SELECT employee_id, extract(resume,'//FULL_NAME/text()') result FROM emp_resumes;
18
11-18 Copyright © 2004, Oracle. All rights reserved. Filtering with extractValue() Searching XML data: Searching relational data: SELECT employee_id, extract(resume,'/RESUME/JOB_HISTORY') result FROM emp_resumes WHERE extractValue(resume,'//JOB_ID') = 'AD_PRES'; SELECT last_name, salary FROM emp_resumes er, employees e WHERE e.employee_id = er.employee_id AND extractValue(er.resume,'//JOB_ID') = e.job_id;
19
11-19 Copyright © 2004, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Describe XMLType Create objects of XMLType Describe XMLType storage options Load data into XMLType Retrieve data from XMLType columns Use XMLType member functions called methods Use XPath functions on XMLType data
20
11-20 Copyright © 2004, Oracle. All rights reserved. Practice 11: Overview This practice covers the following topics: Creating XMLType columns Inserting data into XMLType columns Querying XMLType columns Using existsNode(), extractValue(), and extract()
21
11-21 Copyright © 2004, Oracle. All rights reserved. Practice Notes Page
22
11-22 Copyright © 2004, Oracle. All rights reserved.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.