1 JAXP & XPATH. Objectives 2 ï¶ XPath ï¶ JAXP Processing of XPath ï¶ Workshops

  • View

  • Download

Embed Size (px)

Text of 1 JAXP & XPATH. Objectives 2 ï¶ XPath ï¶ JAXP Processing of XPath...


  • Objectives*XPathJAXP Processing of XPath Workshops

  • XPathIs 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. FeaturesIt is a strongly typed language.It uses sequences, comparisons, quantified expressions, and ranges

  • XPath Data ModelIs 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.

  • XPath Data Model (cont)Root NodeIs 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 NodeAn 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 NodeEach 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 NodeIn a text node, the character data is arranged in a group. Keeps the characters in XML document.

  • XPath Data Model (cont)Comment NodeEvery 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 NodeAre 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 NodeEach 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.

  • Naming & Binding Naming ContextNamespace ContextTo 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 propertiesNamespace URI, where the prefix is bound.Prefix, which is a part of the attribute name.Binding Namespace ContextThe 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

    MethodsDescriptionsgetNamespaceURI- public String getNamespaceURI(String prefix) - Is used in the current scope to get namespace URI bound to a prefixgetPrefix- public String getPrefix(String namespaceURI)- Is used in the current scope to get prefix bound to namespace URIgetPrefixes- public Iterator getPrefixes(String namespaceURI)- Is used in the current scope to get all prefixes bound to a namespace URI

  • JAXP API for XPathIs defined in the javax.xml.xpath packageThe interfaces of JAXP APIs that are used for XPath processing areXPathThe 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.XPathExpressionThe 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 setThe 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

  • Examplepublic 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
  • Example (cont)

    Chris Color TV 5 7500 David DVDPlayer 15 4000 Mitchell Washing Machine 8 10000

  • 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

    MethodsDescriptionssetNamespaceContext- public void setNamespaceContext(NamespaceContext ns) - Is used to set up a namespace contextsetXPathFunctionResolver- public void setXPathFunctionResolver(XPathFunctionResolver resol)- Is used to set up a function resolver

  • Examplepublic 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
  • Example (cont)

    ChristineIllinoisBurtonColorado PetersonNew MexicoCorrine

  • WORKSHOP ACTIVITIESBuilding the console Java application using XPath to Find and count the item element in XML documentParsing naming context to find, count and print the filtered content in XML file

    ***Feasible: thuan tien, tien loi*Feasible: thuan tien, tien loi*Spawn: de trung, tao ra*Request parameter and Request method*Spawn: de trung, tao ra*Spawn: de trung, tao ra*Spawn: de trung, tao ra*Spawn: de trung, tao ra