Home Tutorials XPath Use of name() function in XPath
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Use of name() function in XPath

Advertisement
In the previous section of XPath in Java tutorial we have studied about how to use count() function in XPath query and in our this section we are going to see how we can implement name() function in the XPath query.

Use of name() function in XPath

     

In the previous section of XPath in Java tutorial we have studied about how to use count() function in  XPath query and in our this section we are going to see how we can implement name() function in the XPath query.

First of all we have created an XML file xmlData.xml. This xml file contains various nodes and we have used name() function in our XPathName class to retrieve name of different nodes according to queries:

Here is the full example code of xmlData.xml as follows:

xmlData.xml

<?xml version="1.0" ?>
<AAA>
  <CCC>
  <BBB/>
  <BBB/>
  <BBB/>
  </CCC>
  <DDD>
  <BBB/>
  <BBB/>
  </DDD>
  <EEE>
 <BCC/>
 <BDD/>
  </EEE>
</AAA>

Now we have declared a class XPathName  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 xmlData.xml file in that current working directory.

  DocumentBuilderFactory domFactory = 
  DocumentBuilderFactory.newInstance();

  domFactory.setNamespaceAware(true); 
  DocumentBuilder builder = 
  domFactory.newDocumentBuilder();

  Document doc = builder.parse("xmlData.xml");

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

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

Expression "//*[name()='BBB']" will select all elements with name BBB, equivalent with //BBB, "//*[starts-with(name(),'B')]" will select all elements name of which starts with letter B and "//*[contains(name(),'C')]" will select all elements name of which contain letter C.
Here is the full example code of XPathName.java as follows:

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

public class XPathName {

  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("xmlData.xml");
  XPath xpath = XPathFactory.newInstance().newXPath();
   XPathExpression expr = xpath.compile("//*[name()='BBB']");
  //Select all elements with name BBB, equivalent with //BBB
 System.out
   .println
("Select all elements with " +
  "name BBB, equivalent with //BBB"
);
  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.println(nodes.item(i).getNodeName())
  }
  System.out
  .println
("Select all elements name "+
  "of which starts with letter B"
);
  expr = xpath.compile("//*[starts-with(name(),'B')]");
   //Select all elements name of which starts with letter B
  result = expr.evaluate(doc, XPathConstants.NODESET);
  nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.println(nodes.item(i).getNodeName())
  }
  System.out
  .println(
"Select all elements "+
   "name of which contain letter C"
);
  expr = xpath.compile("//*[contains(name(),'C')]");
   //Select all elements name of which contain letter C
  result = expr.evaluate(doc, XPathConstants.NODESET);
  nodes = (NodeListresult;
  for (int i = 0; i < nodes.getLength(); i++) {
 System.out.println(nodes.item(i).getNodeName())
  }  
  }
}

Follow these steps to run this example :

  • create xmlData.xml
  • create and save XPathName.java
  • compile and execute XPathName class 

Output:

Download Source Code

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 29, 2008

Ask Questions?    Discuss: Use of name() function in XPath  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
iblazei
December 14, 2012
How to select only tag names out of one note?

For example you have such order <root> <AAA> <BBB> <CCC> <DDD> </AAA> <AAA> <BBB> <CCC> <DDD> </AAA> </root> How to select only BBB CCC DDD and display them ONCE (not doubled) in an output document for example in a text document in a row?
DMCA.com