More Tutorials| Bioinformatics| Open Source| Photoshop| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java - XPath Tutorial 
 

In this example we have created an XML file "person.xml" first, which is necessary to execute XPath query on to it.

 

Java - XPath  Tutorial

                         

Showing all elements of an XML file using Java xpath

In this example we have created an XML file "person.xml" first, which is necessary to execute XPath query on to it. This "persons.xml" contains information related to name, age, gender of different persons.

Here is the full source code for persons.xml file as follows :

persons.xml

<?xml version="1.0" ?>
<information>
    <person id="1">
        <name>Deep</name>
        <age>34</age>
        <gender>Male</gender>
    </person>
 
     <person id="2">
        <name>Kumar</name>
        <age>24</age>
        <gender>Male</gender>
    </person>
 
    <person id="3">
        <name>Deepali</name>
        <age>19</age>
        <gender>Female</gender>
    </person>

    <!-- more persons... -->
</information>

Now we have declared a class XPathDemo  and in this class we are parsing the XML file with JAXP. First of all we do need to load the document into DOM Document object. We have placed that persons.xml file in that current working directory.

    DocumentBuilderFactory domFactory = 
    DocumentBuilderFactory.newInstance();

          domFactory.setNamespaceAware(true); 
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("persons.xml");

Above lines of code parses "persons.xml" file and creates a Document object. Next we have created XPath object with the use of XPathFactory.

XPath xpath = XPathFactory.newInstance().newXPath();

Now we compile the path with the use of compile() method. Finally we will evaluate XPath expression and then we can print all those elements.
Here is the example code for XPathDemo.java as follows:

XPathDemo.java

import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;

public class XPathDemo {

  public static void main(String[] args
   throws ParserConfigurationException, SAXException, 
          IOException, XPathExpressionException {

    DocumentBuilderFactory domFactory = 
    DocumentBuilderFactory.newInstance
();
          domFactory.setNamespaceAware(true)
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("persons.xml");
    XPath xpath = XPathFactory.newInstance().newXPath();
       // XPath Query for showing all nodes value
    XPathExpression expr = xpath.compile("//person/*/text()");

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeListresult;
    for (int i = 0; i < nodes.getLength(); i++) {
     System.out.println(nodes.item(i).getNodeValue())
    }
  }
}

To run this example follow these steps as mention below :

  1. First create and save an XML file "persons.xml"
  2. Create XPathDemo.java and compile it.
  3. Execute XPathDemo you will get the following output on your console

Output:

Download Source Code

                         

» View all related tutorials
Related Tags: c text xpath select tutorials path io vi tutorial axis context ria this node element elements tex for ie with

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

Very good n precise tutorial. If you also add how to create the same xml file using the java code n also editing then it will be a one stop webpage for doing basic XML operations for many.

Posted by Anshuman Dhuliya on Friday, 12.5.08 @ 01:43am | #82428

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.