Course XML files with XPath PDF Print E-mail
User Rating: / 0
PoorBest 
Thursday, 15 May 2008

I. Introduction
XPath can navigate an XML file in a way that is both simple and powerful. In this way, in a short time, a developer can quickly and easily retrieve information of interest, even in a document which contains much more. Examples:

retrieve the contents of a specific tag
retrieve content based on the value of an attribute of a tag
retrieve a set of tags with their content and browse
That is what we will detail in this tutorial. After a short presentation of XPath, some existing Java solutions, we will move quickly to practice with explanations and examples using JDom and Jaxen. Finally, you find the source codes and other interesting links at the end of the article.

II. Overview

II-A. What XPath?
For this, nothing like that traverse the XML (XPath), which tells us:

XPath is a language with a syntax not XML, to address the different knots or groups of individuals knots an XML document. XPath sees the XML document as a tree of nodes, it can navigate using axes (son, parent, ancestor, descendant, ...) and selecting nodes by name. The XPath expressions can also represent strings, numbers and Boolean.
XPath is intensely used by other languages and XML technologies such as XSLT, W3C XML Schema, XLink, XForms ...

Other definitions are also available in the Java / XML (XPath) or directly on the site of the W3C.

II-B. Solutions for Java XPath

As often in Java, the choice is vast. Here are some of XPath APIs available for the Java platform:
-- APIs standard: Since J2SE 5.0 (Tiger), the API contains the standard package javax.xml.xpath, which can work with XPath.
-- Jaxen: This API is capable of evaluating XPath expressions on many models (JDOM, DOM4J or DOM).
-- Saxon: Finally, we can cite Saxon, which has the particularity to allow working with the XPath specification 2.0.

For this article, the choice fell on Jaxen, and more specifically the implementation that are found in JDom.

II-C. Environment required

As mentioned above, used bookstores are JDom and Jaxen. Below are the jars that will be required:
-- Jdom.jar: definition of interfaces
-- Jaxen-core.jar: for parsing
-- Jaxen-jdom.jar: implementation of XPath
-- Saxpath.jar: to build the file Dom

For these jars, nothing more simple: just go to the site Jdom. In Downloads / binaries you the link to Jdom 1.0. Download then all Jdom the format of your choice and then unpack it. They include:
-- Under \ build: jdom.jar
-- Under \ lib: other jars of interest to us, namely jaxen-core.jar, jaxen-dom.jar and saxpath.jar

III. Principle of operation

III-A. Overview
The library JDom offers an interface that defines a way to use XPath in Java to browse XML files. The jar jaxen-jdom implements this interface.

The operation is as follows: the document is fully parsed and stored in memory via a document JDom. From this document, we will implement XPath paths to find the elements that concern us. These paths XPath can go to basics, avoiding describe the exact path followed. We will be able to ask searching such as tag or attribute from the current location or the entire document.

Of course, this initial route, and especially the establishment of the Document, is not the fastest approach (see why SAX).

By against side developer, saving time in the course of the XML file is considerable. No more searching the details of the sequence tags, it goes directly to basics!

III-B. The syntax XPath
XPath proposes a language localization, whose basic elements are as follows:

Element syntactic
Type path
Examples way
Examples way responding to the path shown to the left
tag
Name Element
hopital
<hopital>… </ hospital>
/
Separates direct children
hospital / person
<hopital>
<personne>… </ person>
</ hospital>
/ /
Descendant
hospital / / relationships
----
/ / name
<hopital>
<pensionnaires>
<patient>
<relations> ...</ relations>
</ patient>
</ hospital>
----
<hopital>
<personne>
<name> </ name>
</ person>
<hopital>
=> Search anywhere in the scheme
*
"wildcard"
* / title
<bla> <title> .. </ title> or <bli> <title> ...</ title>
|
operator "or"
Title | Head
----
*|/|@*
<title> ...</ title> or <head> ...</ head>
----
(all elements: children, the root and attributes of the root)
.
element
.
.. /
element higher
.. / problem
<project>
@
attribute name
@ id
----
Project / @ ID
<xyz id="test"> ...</ XYZ>
----
<project id="test" ...> ... </ project>
@ attr = 'type'
Value attribute
list [@ type = 'ol']
<list type="ol"> ...... </ list>
For more information, I urged you to look at this excellent pdf: Introduction technical XSLT, including the above table is heavily inspired (with the consent of the authors of this document).

The result of a search can take several forms:
-- The value of the first node responding to the expression,
-- The first node responding to the expression,
-- All the nodes replying to the query.

By node, it means the location of a specific tag in the XML file. Thus, using the ". "Or «..», it is also possible to search from knots or found in a previous search.

As you can see the power of expression of this language is consistent.

III-C. XPath applied to Java
It is a first step in building the DOM document:

/ / It creates an instance of SAXBuilder
SAXBuilder sxb = new SAXBuilder ();
org.jdom.Document document sxb.build = (new File ( "exempleXml.xml"));
/ / It initiates a new root element with the root element of
/ / Document.
root = document.getRootElement ();

It must then declare the road to look, we proceeded as follows:

XPath xpa = XPath.newInstance ( "path");

It then applies this path to a junction of our choice, for example the root. We can choose either:
-- To obtain the return value:

String return xpa.valueOf = (currentNode);

-- To get the first node responding to our request:

Element monNoeud = (Element) xpa.selectSingleNode (root);

-- To get all the nodes responding to our request:

List listeNoeuds = xpa.selectNodes (root);

 
IV. Example implementation
An example is better than many speeches, therefore see what it gives.

IV-A. The XML file considered
For this, we will build on an XML file a bit complex, built as follows:
hopital.xml

<? xml version = "1.0" encoding = "ISO-8859-1"?>
<hopital>
<personne Id="idPersonne1">
<name> Hugo </ name>
<firstname> Victor </ first name>
</ Person>
<personne Id="idPersonne2">
<name> Victor </ name>
<firstname> Paul Emile </ first name>
</ Person>
<personne Id="idPersonne3">
<name> Kontz </ name>
<firstname> Dean R. </ first name>
</ Person>
<personne Id="idPersonne4">
<name> Sand </ name>
<firstname> Georges </ first name>
</ Person>
<personne Id="idPersonne5">
<name> Clark </ name>
<firstname> Mary Higgins </ first name>
</ Person>
<personne Id="idPersonne6">
<name> Weis </ name>
<firstname> Margaret </ first name>
</ Person>
<personne Id="idPersonne7">
Fielding <name> </ name>
<firstname> Helen </ first name>
</ Person>
<personne Id="idPersonne8">
<name> Troyes </ name>
<firstname> Chrétien </ first name>
</ Person>
<pensionnaires>
<patient PersonneId="idPersonne3">
<filiation>
<parent personneId="idPersonne1" type="naturel">
<contact type = "daily" ddebut = "1989/02/16"
Dfin = "2001/12/21"> Children </ contact>
<contact type = "episodic" ddebut = "2001/12/22"
dfin =".."> Divorce of parents, loss of contact
</ Contact>
</ Parent>
<parent personneId="idPersonne4" type="naturel">
<contact type = "regular" ddebut = "1989/02/16"
dfin = "2001/12/21"> Infant Dean R. </ contact>
<Contact type = "none" ddebut = "2001/12/22"
Dfin ="..">
Out among parents, Dean R. no longer refers to his mother.
</ Contact>
</ Parent>
<parent personneId="idPersonne7" type="tuteur">
<contact type = "frequent" ddebut = "2002/02/12"
Dfin =".."> New guardian </ contact>
</ Parent>
</ Filiation>
<internements>
<sejour ddebut="2002/01/11" dfin="2002/01/14">
Crisis appendicitis </ sejour>
</ Internment>
</ Patient>
<patient PersonneId="idPersonne2">
<filiation>
<parent personneId="idPersonne1" type="naturel">
<contact type = "episodic" ddebut = "1985/03/21"
dfin =".."> Father often on the move </ contact>
</ Parent>
<parent personneId="idPersonne5" type="naturel">
<contact type = "daily" ddebut = "1985/03/21"
dfin = "1999/08/11"> Paul Emile lives with his mother </ contact>
<Contact type = "none" ddebut = "1999/08/12"
Dfin =".."> Death </ contact>
</ Parent>
</ Filiation>
<internements>
<sejour ddebut="1998/03/22" dfin="1998/04/12">
Crisis laugh repeated </ sejour>
</ Internment>
</ Patient>
<patient PersonneId="idPersonne6">
<filiation>
<parent personneId="idPersonne8" type="naturel">
<contact type = "regular" ddebut = "1982/12/30"
Dfin =".."> SAR </ contact>
</ Parent>
<parent personneId="idPersonne4" type="naturel">
<contact type = "regular" ddebut = "1982/12/30"
Dfin =".."> SAR </ contact>
</ Parent>
</ Filiation>
<internements>
<sejour ddebut="1998/03/22" dfin="1998/03/25"> broken arm
</ Sejour>
</ Internment>
</ Patient>
</ Boarders>
</ hospital>

I said that this file is only educational purposes only. It is not an example of good construction XML, including the level of <personne> tags. This file is available there.

IV-B. The main class
ExempleXPath.java

org.zedros.xpathxmlreader package;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

/ **
  *
  * @ Author ZedroS
  * /
public class (ExempleXPath
    
     / ** Creates a new instance of ExempleXPath * /
     public ExempleXPath () (
     )

     / ** Parse file past the entrance to read the name of each patient.
      * For that, one looks for patients and then, from their identification, cherche on their behalf.
      *
      * @ Params Filename to read.
      * /
     void parse (File _FilePath) (
         org.jdom.Document document = null;
         try (
             / * It creates an instance of SAXBuilder * /
             SAXBuilder sxb = new SAXBuilder ();
             document = sxb.build (_FilePath);
         ) Catch (IOException e) (
             System.out.println ( "Error reading file"
+ E.getMessage ());
             e.printStackTrace ();
         ) Catch (JDOMException e) (
             System.out.println ( "Error during the construction of file JDOM"
+ E.getMessage ());
             e.printStackTrace ();
         )
        
         try (
             / * Initialize a new element with the root element of
                document. * /
             Element root = document.getRootElement ();
            
             / * It will at first seek all names
                of patients in our hospital. * /
            
             / * Search the list of patients * /
             XPath xpa = XPath.newInstance ( "/ / patient");
            
             / * Gets all the nodes meet the path / / patient * /
             List results = xpa.selectNodes (root);
            
             Iterator iter = results.iterator ();
            
             Element noeudCourant = null;
             String personneId = null;
             while (iter.hasNext ()) (
                 / * For each patient we will seek his name and then display * /
                 noeudCourant = (Element) iter.next ();
                
                 / * Get the ID of the person
                    Grade. at the beginning of the road: on part of the current position
                    @ indicates that it seeks an attribute * /
                 xpa = XPath.newInstance ("./personneId ");
                 personneId = xpa.valueOf (noeudCourant);
                
                 / * From there it gets the info in the corresponding tag person
                    It specifies that research on a tag depending on the value
                    one of its attributes: * /
                 xpa = XPath.newInstance ( "/ / person [@ id =" personneId + + "']");
                 noeudCourant = (Element) xpa.selectSingleNode (noeudCourant);
                
                 / * We are trying to present the value of the tag name: * /
                 xpa = XPath.newInstance ( ". / name");
                 System.out.println ( "Value:" + xpa.valueOf (noeudCourant));
             )
         ) Catch (JDOMException e) (
             System.out.println ( "Error JDOM" + e.getMessage ());
             e.printStackTrace ();
         )
     )
)

This file can be downloaded there.

And finally, the Main class to start it all:
Main.java

org.zedros.xpathxmlreader package;

import java.io.File;
import org.jdom.JDOMException;

/ **
  *
  * @ Author ZedroS
  * /
Main public class (
    
     / ** Creates a new instance of Main * /
     public Main () (
     )
    
     / **
      * @ Param args the command line arguments
      * /
     public static void main (String [] args) throws JDOMException (
         File entryFile = new File ( "d: / Java/xml/hopital01.xml");
         ExempleXPath instance = new ExempleXPath ();
         exemple.parse (entryFile);
     )
    
)

This file can be downloaded there.

V. Conclusion
I hope this example has convinced you of the interest and feasibility of the implementation of XPath via Java code. For my part, I've switched to this approach when I go XML without having any constraints on performance.

Here are against the sources of this article and other valuable resources on Java technology, XML and XPath.

Finally, you can always support you on the forums developpez.com if you have any other questions: forums forums Java and XML.
 
< Prev   Next >
School Joomla Templates and Joomla Tutorials