Help getting started with XPath

Hello,

I am just trying to get going with XML and XPath. I have downloaded java sdk version 1.5 that comes with the XPath implementation. However, there are no examples provided...that I can find.

Could someone provide a quick example of how to implement Sun's version of XPath, or point me to a tutorial that does this, or tell me how to implement one of the other XPath implementations.

Thanks for any and all assistance,

Mike

[460 byte] By [MikeEller] at [2007-9-30 21:59:57]
# 1
http://www.cafeconleche.org/books/xmljava/chapters/ch16.html
SteveNaive at 2007-7-7 3:27:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

A good XPath tutorial is here: http://www.w3schools.com/xpath/

I use the Aapche DOM API XPath implementation which is quite straight forward:. Here's an example:

/**

* Get value of the node specified by an XPath.

*

* @param document Document to get node value of.

* @param xPath XPath to select single node value (e.g. "/root/data/value[1]/text()")

* @return Result of <i>xPath</i> (the node's value) or null if failed.

* @throws XPathException if XPath result object is not of type XPathResult.

* @see <a href="http://www.w3schools.com/xpath/default.asp">XPath Tutorial</a>

*/

static public String getXPathValue(Document document, String xPath) throws XPathException {

String result = null;

if (document != null && xPath != null) {

XPathEvaluator evaluator = new XPathEvaluatorImpl(document);

Object o = evaluator.evaluate(xPath, document, evaluator.createNSResolver(document), XPathResult.FIRST_ORDERED_NODE_TYPE, null);

if (o != null) {

if (o instanceof XPathResult) {

XPathResult xpResult = (XPathResult) o;

Node node = xpResult.getSingleNodeValue();

if (node != null) {

result = node.getNodeValue();

}

} else {

throw new XPathException(XPathException.TYPE_ERR, "XPath '"+xPath+"' didn't return an object of type XPathResult!");

}

}//else: no result for XPath

}//else: input values not available

return result;

}//getXPathValue()

MartinHilpert at 2007-7-7 3:27:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
And here's a code example for 1.5: http://www.ociweb.com/jnb/jnbAug2004.html
MartinHilpert at 2007-7-7 3:27:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
Easy 1.5 example:XPath xpath = XPathFactory.newInstance().newXPath();String expr = "//element[@name='refentry']/content-model";String result = xpath.evaluate(expr, dom);
MartinHilpert at 2007-7-7 3:27:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Hi ,

I am new to xpath and Jdom can any one help me sort out my reequirement.

here I am attaching small portion of my xml file:

Cases

Golf SMS campaign

Tunes and Pix SMS campaign

Top-up SMS being sent to customers

ThreePay television ads July 04

I need to get the cases based on the creation time.

Thanks

Babu H

babu-h at 2007-7-7 3:27:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
@babu-h: first post a valid XML file so we give you the XPath. Read this for a start: http://www.w3schools.com/xpath/
MartinHilpert at 2007-7-7 3:27:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
Hello MartinHilpertDo you know to get the absolute xpath expressions for any node in Document.I want to want an attribute for each Node with its absolute xpath.THankssvkrishna
svkrishna at 2007-7-7 3:27:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8
SOrry for bad english earlierDo you know how to get an absolute xpath expression for any node in a document.I want to add an attribute for each Node with its absolute xpathHas anybody attempted this?
svkrishna at 2007-7-7 3:27:28 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...