Java XPath Parser - Overview


Advertisements


XPath is an official recommendation of the World Wide Web Consortium (W3C). It defines a language to find information in an XML file. It is used to traverse elements and attributes of an XML document. XPath provides various types of expressions which can be used to enquire relevant information from the XML document.

What is XPath?

  • Structure Definations − XPath defines the parts of an XML document like element, attribute, text, namespace, processing-instruction, comment, and document nodes.

  • Path Expressions − XPath provides powerful path expressions such as select nodes or list of nodes in XML documents.

  • Standard Functions − XPath provides a rich library of standard functions for manipulation of string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, etc.

  • Major part of XSLT − XPath is one of the major elements in XSLT standard and one must have sufficient knowledge of XPath in order to work with XSLT documents.

  • W3C recommendation − XPath is official recommendation of World Wide Web Consortium (W3C).

Here is the input text file we need to parse:

<?xml version = "1.0"?>
<class>
   <student rollno = "393">
      <firstname>dinkar</firstname>
      <lastname>kad</lastname>
      <nickname>dinkar</nickname>
      <marks>85</marks>
   </student>
   
   <student rollno = "493">
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>vinni</nickname>
      <marks>95</marks>
   </student>
   
   <student rollno = "593">
      <firstname>jasvir</firstname>
      <lastname>singn</lastname>
      <nickname>jazz</nickname>
      <marks>90</marks>
   </student>
</class>

XPath Expressions

XPath uses a path expression to select node or list of nodes from an XML document. Following is a list of useful paths and expression to select any node/list of nodes from an XML document.

Sr.No. Expression & Description
1

node-name

Select all nodes with the given name "nodename"

2

/

Selection starts from the root node

3

//

Selection starts from the current node that match the selection

4

.

Selects the current node

5

..

Selects the parent of the current node

6

@

Selects attributes

7

student

Example − Selects all nodes with the name "student"

8

class/student

Example − Selects all student elements that are children of class

9

//student

Selects all student elements no matter where they are in the document

Predicates

Predicates are used to find specific node or a node containing specific value and are defined using [...].

Expression Result
/class/student[1] Selects the first student element that is the child of the class element.
/class/student[last()] Selects the last student element that is the child of the class element.
/class/student[last()-1] Selects the last but one student element that is the child of the class element.
//student[@rollno = '493'] Selects all the student elements that have an attribute named rollno with a value of '493'


Advertisements