+ All Categories
Home > Documents > CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San...

CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San...

Date post: 17-Jan-2016
Category:
Upload: alfred-tucker
View: 213 times
Download: 0 times
Share this document with a friend
26
CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak
Transcript
Page 1: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

CS 157B: Database Management Systems IIFebruary 13 Class Meeting

Department of Computer ScienceSan Jose State University

Spring 2013Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

2

XML Namespaces

XML namespaces are similar to Java packages. They prevent element name clashes. An element name can be in the scope of a namespace.

A namespace name must be unique. Use a URI (uniform resource identifier) as the name.

Start with your unique domain name. A URL is a common form of URI.

The URL doesn’t have to point to an actual file.

Declare a namespace in an element tag. The scope of the namespace is that element and its children. Example: A namespace declared in the root element has the

entire XML document in its scope._

Page 3: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

3

XML Namespaces

Example:

This declares the default namespace. All elements in its scope are in the default namespace. Elements not in any namescape scope are “in no namespace”.

Declare a namespace with a prefix:

Non-default namespace. Prefix element names that are in the namespace scope. The element containing the declaration is itself in the scope. The prefix is considered part of the element name.

<library xmlns="http://www.cs.sjsu.edu/cs157b/library">

<au:author xmlns:au="http://www.cs.sjsu.edu/cs157b/author"> ...</au:author>

Page 4: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

4

XML Namespaces

Nested namespaces:

Why are the book and author namespaces necessary? Prevent the book title and the author title name clash.

<library xmlns="http://www.cs.sjsu.edu/cs157b/library" xmlns:bk="http://www.cs.sjsu.edu/cs157b/book" xmlns:au="http://www.cs.sjsu.edu/cs157b/author"> <bk:book> <bk:title>Java Programming</bk:title> <au:author> <au:title>Dr.</au:title> ... </au:author> </bk:book></library>

Page 5: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

5

XML Namespaces

Alternate:

<library xmlns="http://www.cs.sjsu.edu/cs157b/library"> <bk:book xmlns:bk="http://www.cs.sjsu.edu/cs157b/book"> > <title>Java Programming</title> <au:author xmlns:au="http://www.cs.sjsu.edu/cs157b/author"> <title>Dr.</title> ... </au:author> </bk:book></library>

Page 6: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

6

Parsing XML Documents

In order for your Java program to work with an XML document, it must be able to read and understand its contents. We say that a program “parses” an XML document when it

reads the document and decomposes it into elements, attributes, content, etc.

XML has a very simple syntax, which makes XML documents relatively easy to parse.

Java has packages specifically for parsing XML: JAXP DOM parser JAXP SAX parser JAXP StAX parser

JAXP = Java API for XML Processing

Page 7: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

7

DOM Parser

DOM is the Document Object Model. The Java DOM parser parses an XML document

and builds a DOM tree consisting of node objects. The nodes represent elements, attributes, content, etc. Compiler writers would call this a “parse tree”.

Once the DOM tree has been built, your program can navigate the tree and visit each node. node.getNodeType() node.getNodeName() node.getValue() node.getChildNodes() node.getAttributes()

DOMParserDemo

Page 8: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

8

DOM Parser

Advantages Easy to build. Easy to navigate and visit the nodes.

Random access is possible.

You can modify the nodes. You can modify the tree structure.

Disadvantages If the XML document is large, the DOM tree

will take up lots of memory.

Page 9: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

9

SAX Parser

SAX is the Simple API for XML. A SAX parser is a “push” parser.

As it reads an XML document, it pushes an event to your program as it recognizes each element, attribute, content, etc.

Your program contains “event handlers” which are methods that the parser automatically calls as each event occurs.

A SAX parser is in control of your program while it’s parsing a document. “Inversion of control” SAXParserDemo

Page 10: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

10

SAX Parser

Advantages Your event handlers do all the work of processing

each component as the parser encounters it as it reads the document.

Very low memory requirements compared to the DOM parser. There is no tree that represents the entire document in

memory – unless your program builds it.

Disadvantages You process the document’s components in the

order that they’re parsed. You cannot randomly visit the components. Read only – you cannot modify the XML document.

Page 11: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

11

StAX Parser

StAX is the Streaming API for XML. A StAX is a “pull” parser.

Your program remains in control as it parses an XML document.

Your program requests the delivery of parsing events.

Two APIs to navigate an XML document. Cursor API Iterator API

Your program can both read and write XML documents.

Page 12: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

12

StAX Cursor API

Use the cursor API to do a walk-through of the XML components in document order. Lowest-level access to a document’s structure and

content. XMLStreamReader interface.

next() and hasNext() methods to scan a document’s content.

The next() method returns an integer token that represents the next parse event.

Depending on the next event, call the appropriate methods of the interface.

Page 13: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

13

StAX Iterator API

Use the iterator API to access a document’s structure and content in the form of “event objects”.

XMLEventReader interface nextEvent() and hasNext() methods to

iterate over a document’s structure and contents. The nextEvent() method returns an

XMLEvent object. The XMLEvent interface has methods to

determine and process the next event type.

StAXParserDemo

Page 14: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

14

Object-XML Mapping (The Hard Way)

Recall how to do object-relational mapping using JDBC. As your program extracts field values from a row set,

it can create objects with those values. Your program has to explicitly create the objects.

Similarly, your program can use any of the XML parsing APIs to create objects from a document. The objects can represent the elements. Define a different Java class for each element type.

Hibernate automates mapping Java classes to database tables and automatically creates objects at run time. We’ll soon see how to automate object-XML mapping.

_

Page 15: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

15

XPath

XPath views an XML document as a node tree. Everything in the document is a node.

element, attribute, text content

Every node is related to another node. parent, child, ancestor, descendant, sibling

An XPath expression is a location path that walks the tree starting from the root in order to select a single node or a set of nodes. The selection is based on the node relations

and conditional tests on attribute values._

Page 16: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

16

Location Paths

XPath expressions look like Unix file paths. / represents the root node of the document. Add more element names separated by /

to step down the tree.

A location path can select a single node in the tree or a set of nodes._

Page 17: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

17

Location Path Examples<?xml version="1.0" encoding="UTF-8"?> <catalog xmlns:journal="http://www.apress.com/catalog/journal" > <jrnl:journal title="XML" publisher="IBM developerWorks"> <article level="Intermediate" date="February-2003"> <title>Design XML Schemas Using UML</title> <author>Ayesha Malik</author> </article> </jrnl:journal> <journal title="Java Technology" publisher="IBM developerWorks"> <article level="Advanced" date="January-2004"> <title>Design service-oriented architecture frameworks with J2EE technology</title> <author>Naveen Balani</author> </article> <article level="Advanced" date="October-2003"> <title>Advance DAO Programming</title> <author>Sean Sullivan</author> </article> </journal> </catalog>

XML document adapted from the bookPro XML Development with Java Technology,by Ajay Vohra and Deepak Vohra, Apress, 2006

Page 18: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

18

Location Path Examples

/catalog returns the entire document tree. /catalog/journal/article returns all the article nodes. /catalog/journal/* returns all the child nodes of journal nodes.

<?xml version="1.0" encoding="UTF-8"?> <catalog xmlns:journal="http://www.apress.com/catalog/journal" > <jrnl:journal title="XML" publisher="IBM developerWorks"> <article level="Intermediate" date="February-2003"> <title>Design XML Schemas Using UML</title> <author>Ayesha Malik</author> </article> </jrnl:journal> <journal title="Java Technology" publisher="IBM developerWorks"> <article level="Advanced" date="January-2004"> <title>Design service-oriented architecture frameworks with J2EE technology</title> <author>Naveen Balani</author> </article> <article level="Advanced" date="October-2003"> <title>Advance DAO Programming</title> <author>Sean Sullivan</author> </article> </journal> </catalog>

XPathDemo

Page 19: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

19

Location Path Examples

//title returns all title nodes. // means “all descendants of the root node”.

//@date returns all date attributes.

<?xml version="1.0" encoding="UTF-8"?> <catalog xmlns:journal="http://www.apress.com/catalog/journal" > <jrnl:journal title="XML" publisher="IBM developerWorks"> <article level="Intermediate" date="February-2003"> <title>Design XML Schemas Using UML</title> <author>Ayesha Malik</author> </article> </jrnl:journal> <journal title="Java Technology" publisher="IBM developerWorks"> <article level="Advanced" date="January-2004"> <title>Design service-oriented architecture frameworks with J2EE technology</title> <author>Naveen Balani</author> </article> <article level="Advanced" date="October-2003"> <title>Advance DAO Programming</title> <author>Sean Sullivan</author> </article> </journal> </catalog>

Page 20: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

20

Location Path Examples

/catalog/journal/article[@level='Advanced']/title Title nodes of all journal articles at the advanced level. Also:

<?xml version="1.0" encoding="UTF-8"?> <catalog xmlns:journal="http://www.apress.com/catalog/journal" > <jrnl:journal title="XML" publisher="IBM developerWorks"> <article level="Intermediate" date="February-2003"> <title>Design XML Schemas Using UML</title> <author>Ayesha Malik</author> </article> </jrnl:journal> <journal title="Java Technology" publisher="IBM developerWorks"> <article level="Advanced" date="January-2004"> <title>Design service-oriented architecture frameworks with J2EE technology</title> <author>Naveen Balani</author> </article> <article level="Advanced" date="October-2003"> <title>Advance DAO Programming</title> <author>Sean Sullivan</author> </article> </journal> </catalog>

/child::catalog/child::journal/child::article[attribute::level='Advanced']/child::title

Page 21: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

21

Location Path Examples

/catalog/journal[@title='Java Technology']/article All article nodes in journals with title “Java Technology”.

/catalog/journal/article[2]

<?xml version="1.0" encoding="UTF-8"?> <catalog xmlns:journal="http://www.apress.com/catalog/journal" > <jrnl:journal title="XML" publisher="IBM developerWorks"> <article level="Intermediate" date="February-2003"> <title>Design XML Schemas Using UML</title> <author>Ayesha Malik</author> </article> </jrnl:journal> <journal title="Java Technology" publisher="IBM developerWorks"> <article level="Advanced" date="January-2004"> <title>Design service-oriented architecture frameworks with J2EE technology</title> <author>Naveen Balani</author> </article> <article level="Advanced" date="October-2003"> <title>Advance DAO Programming</title> <author>Sean Sullivan</author> </article> </journal> </catalog>

Page 22: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

22

Location Path Examples

//article[ancestor::journal[@title='Java Technology']] All article nodes whose ancestor is a journal with title “Java Technology”.

<?xml version="1.0" encoding="UTF-8"?> <catalog xmlns:journal="http://www.apress.com/catalog/journal" > <jrnl:journal title="XML" publisher="IBM developerWorks"> <article level="Intermediate" date="February-2003"> <title>Design XML Schemas Using UML</title> <author>Ayesha Malik</author> </article> </jrnl:journal> <journal title="Java Technology" publisher="IBM developerWorks"> <article level="Advanced" date="January-2004"> <title>Design service-oriented architecture frameworks with J2EE technology</title> <author>Naveen Balani</author> </article> <article level="Advanced" date="October-2003"> <title>Advance DAO Programming</title> <author>Sean Sullivan</author> </article> </journal> </catalog>

Page 23: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

23

Location Path Examples

//article[preceding-sibling::article] All article nodes that have an earlier (to the left) sibling that’s an article.

//article[following-sibling::article[@date='October-2003']] //author[. = 'Sean Sullivan']/ancestor::journal

<?xml version="1.0" encoding="UTF-8"?> <catalog xmlns:journal="http://www.apress.com/catalog/journal" > <jrnl:journal title="XML" publisher="IBM developerWorks"> <article level="Intermediate" date="February-2003"> <title>Design XML Schemas Using UML</title> <author>Ayesha Malik</author> </article> </jrnl:journal> <journal title="Java Technology" publisher="IBM developerWorks"> <article level="Advanced" date="January-2004"> <title>Design service-oriented architecture frameworks with J2EE technology</title> <author>Naveen Balani</author> </article> <article level="Advanced" date="October-2003"> <title>Advance DAO Programming</title> <author>Sean Sullivan</author> </article> </journal> </catalog>

Page 24: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

24

XPath Axes

child:: Shorthand: just the element name of the child

descendant:: Shorthand: //

attribute:: Shorthand: @

self:: descendant-or-self:: following-sibling:: preceding-sibling:: following::

parent:: ancestor:: preceding:: ancestor-or-self:: namespace::

Page 25: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

25

XPath Expressions and Functions

XPath expressions can also include arithmetic comparisons let (local variables) if, for, some, every

XPath functions include count() format-number(), round-number() substring-before(), substring-after() contains() string-length() translate()

Exercise for the reader!

Page 26: CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.

Department of Computer ScienceSpring 2013: February 13

CS 157B: Database Management Systems II© R. Mak

26

XPath and Java

Create an XPath object:

Parse an XML document with the DOM parser:

Evaluate an XPath expression:

You can also pre-compile an XPath expression. Similar to a JDBC prepared statement.

XPathFactory factory = XPathFactory.newInstance();XPath xPath = factory.newXPath();

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();Document document = builder.parse(xmlFile);

String expr = "//title";xPath.reset();NodeList nodeList = (NodeList) xPath.evaluate(expr, document, XPathConstants.NODESET);


Recommended