Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron."— Presentation transcript:

1 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

2 Department of Computer Science Spring 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. _

3 Department of Computer Science Spring 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....

4 Department of Computer Science Spring 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"> Java Programming Dr....

5 Department of Computer Science Spring 2013: February 13 CS 157B: Database Management Systems II © R. Mak 5 XML Namespaces  Alternate: > Java Programming Dr....

6 Department of Computer Science Spring 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

7 Department of Computer Science Spring 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

8 Department of Computer Science Spring 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.

9 Department of Computer Science Spring 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

10 Department of Computer Science Spring 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.

11 Department of Computer Science Spring 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.

12 Department of Computer Science Spring 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.

13 Department of Computer Science Spring 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

14 Department of Computer Science Spring 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. _

15 Department of Computer Science Spring 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. _

16 Department of Computer Science Spring 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. _

17 Department of Computer Science Spring 2013: February 13 CS 157B: Database Management Systems II © R. Mak 17 Location Path Examples Design XML Schemas Using UML Ayesha Malik Design service-oriented architecture frameworks with J2EE technology Naveen Balani Advance DAO Programming Sean Sullivan XML document adapted from the book Pro XML Development with Java Technology, by Ajay Vohra and Deepak Vohra, Apress, 2006

18 Department of Computer Science Spring 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. Design XML Schemas Using UML Ayesha Malik Design service-oriented architecture frameworks with J2EE technology Naveen Balani Advance DAO Programming Sean Sullivan XPathDemo

19 Department of Computer Science Spring 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. Design XML Schemas Using UML Ayesha Malik Design service-oriented architecture frameworks with J2EE technology Naveen Balani Advance DAO Programming Sean Sullivan

20 Department of Computer Science Spring 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: Design XML Schemas Using UML Ayesha Malik Design service-oriented architecture frameworks with J2EE technology Naveen Balani Advance DAO Programming Sean Sullivan /child::catalog/child::journal/child::article[attribute::level='Advanced']/child::title

21 Department of Computer Science Spring 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] Design XML Schemas Using UML Ayesha Malik Design service-oriented architecture frameworks with J2EE technology Naveen Balani Advance DAO Programming Sean Sullivan

22 Department of Computer Science Spring 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”. Design XML Schemas Using UML Ayesha Malik Design service-oriented architecture frameworks with J2EE technology Naveen Balani Advance DAO Programming Sean Sullivan

23 Department of Computer Science Spring 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 Design XML Schemas Using UML Ayesha Malik Design service-oriented architecture frameworks with J2EE technology Naveen Balani Advance DAO Programming Sean Sullivan

24 Department of Computer Science Spring 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::

25 Department of Computer Science Spring 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!

26 Department of Computer Science Spring 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);


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

Similar presentations


Ads by Google