0% found this document useful (0 votes)
6 views

XML Databases Notes

Database

Uploaded by

novatuswallace
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

XML Databases Notes

Database

Uploaded by

novatuswallace
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

XML Databases

CSS321 - Advanced Database


Management Systems and
Implementation
Outline
 What is XML?
 Syntax of XML Document
 DTD (Document Type Definition)
 XML Schema
 XML APIs
 XML Query Language
 XML Databases
 Oracle JDBC
Introduction to XML
 XML stands for EXtensible Markup Language
 XML was designed to describe data.
 XML tags are not predefined unlike HTML
 XML DTD and XML Schema define rules to
describe data
 XML example of semi structured data
Building Blocks of XML
 Elements (Tags) are the primary components of XML
documents.
Element FNAME nested inside
<AUTHOR id = 123> element Author.
Element <FNAME> JAMES</FNAME>
Author with <LNAME> RUSSEL</LNAME>
Attr id </AUTHOR>

<!- I am comment ->

 Attributes provide additional information about Elements.


Values of the Attributes are set inside the Elements
 Comments stats with <!- and end with ->
XML DTD
 A DTD is a set of rules that allow us to specify
our own set of elements and attributes.
 DTD is grammar to indicate what tags are
legal in XML documents. c
 XML Document is valid if it has an attached
DTD and document is structured according to
rules defined in DTD.
DTD Example
<BOOKLIST> <!DOCTYPE BOOKLIST[
<BOOK GENRE = “Science” <!ELEMENT BOOKLIST(BOOK)*>
FORMAT = “Hardcover”> <!ELEMENT BOOK(AUTHOR)>
<AUTHOR> <!ELEMENT
<FIRSTNAME> AUTHOR(FIRSTNAME,LASTNAM
RICHRD E)>
</FIRSTNAME> <!ELEMENT
<LASTNAME> KARTER FIRSTNAME(#PCDATA)>
</LASTNAME> <!ELEMENT>LASTNAME(#PCDATA)
</AUTHOR> >
</BOOK> <!ATTLIST BOOK GENRE
</BOOKS> (Science|Fiction)#REQUIRED>
<!ATTLIST BOOK FORMAT
Xml Document And (Paperback|Hardcover)
Corresponding DTD “PaperBack”>]>
Bank DTD

<!DOCTYPE bank [
<!ELEMENT bank ( ( account | customer | depositor)+)>
<!ELEMENT account (account_number branch_name
balance)>
<! ELEMENT customer(customer_name customer_street
customer_city)>
<! ELEMENT depositor (customer_name account_number)>
<! ELEMENT account_number (#PCDATA)>
<! ELEMENT branch_name (#PCDATA)>
<! ELEMENT balance(#PCDATA)>
<! ELEMENT customer_name(#PCDATA)>
<! ELEMENT customer_street(#PCDATA)>
<! ELEMENT customer_city(#PCDATA)>
]>
XML Schema
 Serves same purpose as database schema
 Schemas are written in XML
 Set of pre-defined simple types (such as
string, integer)
 Allows creation of user-defined complex
types
XML Schema
 RDBMS Schema (s_id integer, s_name string, s_status string)
 XMLSchema

<Students> <xs:schema>
<Student id=“p1”> <xs:complexType name = “StudnetType”>
<Name>Allan</Name> <xs:attribute name=“id” type=“xs:string” />
<Age>62</Age> <xs:element name=“Name” type=“xs:string />
<Email>allan@abc.com <xs:element name=“Age” type=“xs:integer” />
</Email> <xs:element name=“Email” type=“xs:string” />
</Student> </xs:complexType>
</Students> <xs:element name=“Student”
type=“StudentType” />
XML Document and Schema </xs:schema>
XML APIs: DOM and SAX
 DOM (Document Object Model)
 Provides a definitive API for accessing hierarchical
description languages like XML, HTML
 Specifies interfaces for accessing all part of a
document
 Includes inheritance, typing, and constants
 SAX (Simple API for XML)
 An event-driven parser API
 API reports parsing events to application via
callbacks
 Optimized for parsing large documents by
eliminating need to keep tree structure in memory
XML Query Languages
 Requirement
Same functionality as database query
languages (such as SQL) to process Web
data
 Advantages
 Query selective portions of the document (no
need to transport entire document)
 Smaller data size mean lesser
communication cost
XQuery
 XQuery to XML is same as SQL to RDBMS
 Most databases supports XQuery
 XQuery is built on XPath operators (XPath is
a language that defines path expressions to
locate document data)
XPath Example
<Student id=“s1”>
<Name>John</Name>
<Age>22</Age>
<Email>jhn@xyz.com</Email>
</Student>
XPath: /Student[Name=“John”]/Email
Extracts: <Email> element with value
“jhn@xyz.com”
Oracle and XML
 XML Support in Oracle

XDK (XML Developer Kit)


XML Parser for PL/SQL
XPath
XSLT
Oracle and XML
 XML documents are stored as XML Type ( data type
for XML ) in Oracle
 Internally CLOB is used to store XML
 To store XML in database create table with one
XMLType column
 Each row will contain one of XML records from XML
document
 Database Table: XML Document
 Database Row : XML Record
Examples
<Patients>
<Patient id=“p1”>
<Name>John</Name>
<Address>
<Street>120 Northwestern Ave</Street>
</Address>
</Patient>
<Patient id=“p2”>
<Name>Paul</Name>
<Address>
<Street>120 N. Salisbury</Street>
</Address>
</Patient>
</Patients>
Example
 Create table prTable(patientRecord XMLType);
 DECLARE
 prXML CLOB;
 BEGIN
 -- Store Patient Record XML in the CLOB variable
 prXML := '<Patient id=“p1">
 <Name>John</Name>
 <Address>
 <Street>120 Northwestern Ave</Street>
 </Address>
 </Patient>‘ ;
 -- Now Insert this Patient Record XML into an XMLType column
 INSERT INTO prTable (patientRecord) VALUES
(XMLTYPE(prXML));
 END;
Example
TO PRINT PATIENT ID of ALL PATIENTS

SELECT
EXTRACT(p.patientRecord,
'/Patient/@id').getStringVal()
FROM prTable p;

USE XPATH
Oracle JDBC
 JDBC an API used for database connectivity
 Creates Portable Applications
 Basic Steps to develop JDBC Application
 Import JDBC classes (java.sql.*).
 Load JDBC drivers
 Connect and Interact with database
 Disconnect from database
Oracle JDBC
 DriverManager provides basic services to
manage set of JDBC drivers
 Connection object sends queries to database
server after a connection is set up
 JDBC provides following three classes for
sending SQL statements to server
 Statement SQL statements without parameters
 PreparedStatement SQL statements to be executed
multiple times with different parameters
 CallableStatement Used for stored procedures
Oracle JDBC
 SQL query can be executed using any of the
objects.
(Statement,PreparedStatement,CallableStatement)
 Syntax (Statement Object )
Public abstract ResultSet executeQuery(String sql) throws
SQLException

 Syntax (PreparedStatement,CallableStatement Object )


Public abstract ResultSet executeQuery() throws SQLException

 Method executes SQL statement that returns


ResultSet object (ResultSet maintains cursor
pointing to its current row of data. )
Oracle JDBC (Example)
Import java.sql.*;
Import java.io;
Class simple{
public static void main(String[] args) throws Exception{
Connection conn=null;
try{
String conStr = "jdbc:oracle:thin:@oracle.cs.purdue.edu:1521:orb";
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(conStr,”username”,”passwd");
Statement cursor = conn.createStatement(); // Connection Est.
ResultSet rset = stmt.executeQuery(“Select* from table_name”);
while(orset.next()){
System.out.println(“Printing column name ”+orest.getStringVal(1));
}
}Catch(ClassNotFoundException e){}
cursor.close();
conn.close();
}
}
Industry XML Databases
 ObjectStore eXcelon
 Middle tier server that imports from different DB
stores
 XQL queries applied to integrated data
 Provides “cache server” for XML imported from
heterogeneous DB backends
 Focuses on web applications as access methods
to DBs
 Poet XML Repository
 Object oriented database with standard DB
functionality, with OQL
 Focuses on use of XML to faciliate EDI
Thank You

You might also like