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

Use of @ for attribute in XPath

In the previous section of XPath tutorial we have studied how to show all elements of an XML file.

Use of  @ for attribute in XPath

                         

In the previous section of XPath tutorial we have studied how to show all elements of an XML file. In this section we are describing you the use of @ for attribute in the XPath query. The "@" selects attribute.

In this example we have created an XML file "person.xml" as in our previous section, 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 XPathAttribute  and in this class we are parsing the XML file with JAXP. First of all we need to load the document into DOM Document object. We have put 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();


We want to show all elements with attribute id="3" of the person so we can write this in the XPath query as :

"//person[@id='3']/*" -it will fetch all elements with person id="3" .
Full example code is given as below:

XPathAttribute.java

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

public class XPathAttribute{

  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();
    XPathExpression expr =
    xpath.compile
("//person[@id='3']/*/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 :

  • create and save persons.xml
  • create and save XPathAttribute.java and
  • compile XPathAttribute and after compilation execute it

output would be like this:

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 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
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

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

Copyright © 2008. All rights reserved.