开发者

java XPath api reference

开发者 https://www.devze.com 2023-04-06 11:26 出处:网络
The java standard tutorial http://download.orac开发者_JS百科le.com/javase/tutorial/jaxp/xslt/xpath.html explains the XPath standard itself but not the java api for XPath. I need:

The java standard tutorial http://download.orac开发者_JS百科le.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.


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.


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.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号