File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes XPath Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic

XPath Problem

Jason Gu

Joined: Apr 11, 2005
Posts: 4
<?xml version="1.0" encoding="UTF-8" ?>





What I want to do is create a set of "Book" nodes then use XPath to extract book data from each Book Node and match book titles with prices.


import org.xml.sax.SAXException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPath;

import static java.lang.System.out;

public class XMLTest {
private DocumentBuilder docBuilder;
private XPath xPath;

public XMLTest() {
try {
docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
xPath = XPathFactory.newInstance().newXPath();

} catch (ParserConfigurationException ex) {

public Node evaluateAndReturnNode(String xPathExpression, final Node node) throws XPathExpressionException {
return (Node) xPath.evaluate(xPathExpression, node, XPathConstants.NODE);

public NodeList evaluateAndReturnNodeList(String xPathExpression, final File file)
throws FileNotFoundException, IOException, SAXException, XPathExpressionException {

return (NodeList) xPath.evaluate(xPathExpression, docBuilder.parse(file), XPathConstants.NODESET);

public static void main(String[] args) throws IOException, XPathExpressionException, SAXException {
XMLTest xmlTest = new XMLTest();
File file = new File(".\\test_data\\xml_test.xml");

NodeList nodeList = xmlTest.evaluateAndReturnNodeList("/Books/Book", file);

for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);

out.println("Title: \"" + xmlTest.evaluateAndReturnNode("//Title/text()", node) + "\"");
out.println("Price: \"" + xmlTest.evaluateAndReturnNode("//Price/text()", node) + "\"");



Title: "[#text: Book-1]"
Price: "[#text: $50.00]"
Title: "[#text: Book-1]"
Price: "[#text: $50.00]"
Title: "[#text: Book-1]"
Price: "[#text: $50.00]"



how come only data from the 1st Book got printed?


replacing the code above with the following will get you all the book titles and prices, but there's NO way to match the two.

NodeList titleNodeList = xmlTest.evaluateAndReturnNodeList("/Books/Book/Title/text()", file);

for (int i = 0; i < titleNodeList.getLength(); i++) {
Node titleNode = titleNodeList.item(i);
out.println("Title: \"" + titleNode.getNodeValue() + "\"");

NodeList priceNodeList = xmlTest.evaluateAndReturnNodeList("/Books/Book/Price/text()", file);

for (int i = 0; i < titleNodeList.getLength(); i++) {
Node priceNode = priceNodeList.item(i);
out.println("Price: \"" + priceNode.getNodeValue() + "\"");

it is urgent, please help

[ August 14, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63870

Please do not cross-post the same question in multiple forums. It wastes people's time when multiple redundant conversations take place.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: XPath Problem
It's not a secret anymore!