AgostinoX
AgostinoX

Reputation: 7683

java XPath api reference

The java standard tutorial http://download.oracle.com/javase/tutorial/jaxp/xslt/xpath.html explains the XPath standard itself but not the java api for XPath. I need:

1) I know XPath syntax, that is how to reference /person/dateofbirth element. What is the java api to select this node and get its text content?
2) can I obtain DOM objects via XPath?
3) is somewhere an official tutorial to the api and not to the xpath itself?
The real question is the 3rd point since the issue is having official and first-hand information as a base.

Upvotes: 1

Views: 4614

Answers (3)

gioele
gioele

Reputation: 10205

If you prefer a simpler API, have a look at XPathAPI library.

It is easier to use than the regular Java API and has no dependencies. It also supports List<Node> while the native Java API supports only the non-generic NodeList.

Examples:

Node user = XPathAPI.selectSingleNode(doc, "//user[@id='{}']", userID);

List<String> titles = XPathAPI.selectNodeListAsStrings(doc, "//title");

List<Node> friends =
    XPathAPI.selectListOfNodes(doc, "//user[@id='{}']/friend", userID);

(Disclaimer: I'm the author of the library.)

Upvotes: 1

forty-two
forty-two

Reputation: 12817

That tutorial is about XSLT and th use of XPath in that context. I guess the most "official" documentation of the XPath API is this: http://download.oracle.com/javase/6/docs/api/index.html?javax/xml/xpath/package-summary.html

It is really very simple.

Upvotes: 1

Narendra Yadala
Narendra Yadala

Reputation: 9664

Check these articles, they explain parsing a xml document using XPath api in Java.

http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html

http://onjava.com/pub/a/onjava/2005/01/12/xpath.html

http://www.javaworld.com/javaworld/jw-09-2000/jw-0908-xpath.html

I am not sure if a official tutorial for the API exists.

Upvotes: 1

Related Questions