1 / 14

JAXP & XPATH

JAXP & XPATH. Objectives. XPath JAXP Processing of XPath Workshops. XPath. Is needed to efficiently access the XML data located in different nodes in a database. Is a query language, which uses path expressions to traverse through XML documents. Features

Download Presentation

JAXP & XPATH

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAXP & XPATH

  2. Objectives • XPath • JAXP Processing of XPath • Workshops

  3. XPath • Is needed to efficiently access the XML data located in different nodes in a database. • Is a query language, which uses path expressions to traverse through XML documents. • Features • It is a strongly typed language. • It uses sequences, comparisons, quantified expressions, and ranges

  4. XPath Data Model • Is a concept, which traverses the tree model and divides each XML document into seven types of nodes. • The XPath query works on an XML document as a tree. • In the XPath data model, each node has a string value. Also, the names used for attributes, elements, and namespace nodes are divided into two types, local names and namespace names. • XPath data model has no contiguous text node.

  5. XPath Data Model (cont) • Root Node • Is the root of the tree. • Does not occur anywhere except as the root of the tree. • The purpose of root node is it binds all other nodes in the XML document. • Element Node • An element node associated with each element. • The text nodes and subordinate al nodes are the child nodes of an element in a single place in XML document. • Attribute Node • Each element has a connected set of attribute nodes. • Can never be child of its parent element. This is because it does not consider the element bearing an attribute as the parent of the attribute. • The purpose of attribute node is to keep the attributes in a single place in XML document. • Text Node • In a text node, the character data is arranged in a group. • Keeps the characters in XML document.

  6. XPath Data Model (cont) • Comment Node • Every comment has a comment node in the XML document. But this rule is not applicable if the comment takes place within the document type declaration. • The purpose of comment node is to keep the comment in a single place in XML document. • Processing Instruction Node • Are included in the XML document prolog, normally considered as a header. The prolog is a component of XML document, which consists of DTD and declaration. • Every processing instruction has a processing instruction node in the XML document. But this rule is not applicable if the Processing Instruction takes place within the document type declaration. • Namespace Node • Each element has a connected set of namespace nodes. • An element have a namespace node for every attribute on that element whose name starts with xmlns:. • The purpose of namespace node is to keep the elements with the namespace node.

  7. Naming & Binding Naming Context • Namespace Context • To use a namespace context in a query, you must declare the namespace in the XML • More than one namespace context can be declared, if needed, in a query. • The NamespaceContext interface is used for reading the XML namespace context processing. • Some properties • Namespace URI, where the prefix is bound. • Prefix, which is a part of the attribute name. • Binding Namespace Context • The namespace URI can be bound to more than one prefixes in the current scope. But a prefix can bound to a single namespace URI in the current scope

  8. Is defined in the javax.xml.xpath package The interfaces of JAXP APIs that are used for XPath processing are XPath The XPath interface gives an easy and robust syntax for traversing through the nodes in an XML document. Using XPath, you can change a node in a DOM tree to string or Boolean. XPathExpression The XPathExpression interface deals with location path and predicates. Predicates are symbols like // or /, which allows you to elaborate the nodes selected by XPath. In an XPath expression, you can include XPath variables. The variables used in XPath are called as XPath variables. A reference to an XPath variable starts with “$” and can only occur in match, test, get, and set The classes of JAXP APIs used for XPath processing are: XPathFactory: the XPathFactory class is used for creating XPath objects. XPathConstants: the XPathConstants class defines the data types such as Boolean, NodeSet, number, and string for working with nodes in and XML document JAXP API for XPath

  9. public class XPathEx { public static void main (String[] args) { int count = 0; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = dbf.newDocumentBuilder (); Document doc = builder.parse (new File(args[0])); XPathFactory xpf = XPathFactory.newInstance (); XPath xpath = xpf.newXPath (); NodeList list = (NodeList)xpath.evaluate ("//customers/customer/item", doc, XPathConstants.NODESET); for(int i=0; i<list.getLength (); i++){ if(list.item (i).getTextContent ().equalsIgnoreCase (args[1])) count++; } System.out.println("Out of " + list.getLength () + " customers " + count + " customers have place an order for " + args[1]); }catch(Exception e){ e.printStackTrace ();} } } Example

  10. <?xml version="1.0" encoding="UTF-8"?> <customers> <customer> <name>Chris</name> <item>Color TV</item> <quantity>5</quantity> <price>7500</price> </customer> <customer> <name>David</name> <item>DVDPlayer</item> <quantity>15</quantity> <price>4000</price> </customer> <customer> <name>Mitchell</name> <item>Washing Machine</item> <quantity>8</quantity> <price>10000</price> </customer> </customers> Example (cont)

  11. Processing Namespace Context • JAXP API provides the NamespaceContext interface to establish the namespace relationship when an XPath expression is evaluated. • In a context document, the XPath expression selects all the elements from attributes and namespaces

  12. public class NamespaceContextEx { public static void main (String[] args) { try{ XPath xpath = XPathFactory.newInstance ().newXPath (); xpath.setNamespaceContext (new PersonalNamespaceContext()); String expression = "//Aptech/student/grad:name/text()"; InputSource input = new InputSource(args[0]); NodeList list = (NodeList)xpath.evaluate (expression, input, XPathConstants.NODESET); System.out.println("List of grad students: total - " + list.getLength ()); for(int i=0; i<list.getLength (); i++){ System.out.println("\t" + list.item (i).getNodeValue ()); } }catch(Exception e){} } } Example

  13. <?xml version="1.0" encoding="UTF-8"?> <Aptech xmlns:grad="http://www.aptech.com/grad"> <student> <name>Christine</name> <location>Illinois</location> </student> <student> <grad:name>Burton</grad:name> <grad:location>Colorado</grad:location> </student> <student> <name>Peterson</name> <location>New Mexico</location> </student> <student> <grad:name>Corrine</grad:name> <grad:location></grad:location> </student> </Aptech> Example (cont)

  14. WORKSHOP ACTIVITIES • Building the console Java application using XPath to • Find and count the item element in XML document • Parsing naming context to find, count and print the filtered content in XML file

More Related