• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XPath Problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<?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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to the XML forum.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic