1 jaxp & xpath. objectives 2 xpath jaxp processing of xpath workshops

14
1 JAXP & XPATH JAXP & XPATH

Upload: lambert-simmons

Post on 17-Jan-2016

235 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

1

JAXP & XPATH JAXP & XPATH

Page 2: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

Objectives

2

XPathJAXP Processing of XPath Workshops

Page 3: 1 JAXP & XPATH. Objectives 2  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 It is a strongly typed language. It uses sequences, comparisons, quantified expressions, and ranges

Page 4: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

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.

Page 5: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

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.

Page 6: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

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.

Page 7: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

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 scopeMethods Descriptions

getNamespaceURI- public String getNamespaceURI(String prefix)

- Is used in the current scope to get namespace URI bound to a prefix

getPrefix- public String getPrefix(String namespaceURI)

- Is used in the current scope to get prefix bound to namespace URI

getPrefixes- public Iterator getPrefixes(String namespaceURI)

- Is used in the current scope to get all prefixes bound to a namespace URI

Page 8: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

JAXP API for XPath

Is defined in the javax.xml.xpath packageThe 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

Page 9: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

Example

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 ();} }}

Page 10: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

Example (cont)

<?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>

Page 11: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

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

Methods Descriptions

setNamespaceContext- public void setNamespaceContext(NamespaceContext ns)

- Is used to set up a namespace context

setXPathFunctionResolver- public void setXPathFunctionResolver(XPathFunctionResolver resol)

- Is used to set up a function resolver

Page 12: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

Example

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){} }}

Page 13: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

Example (cont)

<?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>

Page 14: 1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops

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