wood burning stoves 2.0*
The moose likes XML and Related Technologies 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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XPath Problem" Watch "XPath Problem" New topic
Author

XPath Problem

Jason Gu
Greenhorn

Joined: Apr 11, 2005
Posts: 4
<?xml version="1.0" encoding="UTF-8" ?>
<Books>
<Book>
<Title>Book-1</Title>
<Price>$50.00</Price>
</Book>

<Book>
<Title>Book-2</Title>
</Book>

<Book>
<Title>Book-3</Title>
<Price>$100.00</Price>
</Book>
</Books>

-------------------------------------------------------

Description:

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 java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;

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) {
ex.printStackTrace();
}
}

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) + "\"");
}
}
}

-------------------------------------------------------

output:

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

-------------------------------------------------------

Problem:

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

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

Joined: Jan 10, 2002
Posts: 60050
    
  65

Moved to the XML forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Please read this.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

First you get the list of nodes that are "/Books/Book", there are three nodes in that list. Then for each of them you get the first node that is "//Title/text()". Now in XPath "//" means "start at the root", so making that relative to one of the Book elements is pointless. If you had used "Title/text()" instead, that would have been relative to the Book element.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XPath Problem
 
Similar Threads
reading xml elements
XPath Problem
simple web service client
Parsing XML file using Xpath in jdk1.4
XPath Problem