aspose file tools*
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
JavaRanch » Java Forums » Java » Java in General
Reply locked 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: 61662
    
  67

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] [Bear's FrontMan] [About Bear] [Books by Bear]
 
 
subject: XPath Problem