Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Tutorials XPath Use of "|" separator for selecting more than one path

Related Tutorials


 
 

Share on Google+Share on Google+

Use of "|" separator for selecting more than one path

Advertisement
You can execute multiple expressions on an XML file with the use of XPath "|" separator. It allows you more flexibility by allowing you to execute multiple expressions at a time.

Use of  "|" separator for selecting more than one path

     

You can execute multiple expressions on an XML file with the use of XPath "|" separator. It allows you more flexibility by allowing you to execute multiple expressions at a time.

In this example we have created an XML file "persons.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 XPathSeparator  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();

When we will execute the following expression then it will execute both expressions "//person/name/text()" as well as "//person/age/text()".
Here is the full example code for XPathSeparator.java as follows:

XPathSeparator.java

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

public class XPathSeparator {

  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/name/text() | //person/age/text()");
 // Selecting more than one path with | separator
  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())
  }  
  }
}

Output:

To run this example :

  • create and save persons.xml file
  • create and save XPathSeparator.java
  • compile and execute XPathSeparator class and you will get following output on your screen.


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

Related Tutorials

Discuss: Use of "|" separator for selecting more than one path  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com