XPATH
(XML Path Language) is a language for selecting nodes from an XML document. In addition, XPath may be used to compute values (strings, numbers, or boolean values) from the content of an XML document. The current version of the language is XPath 2.0, but because version 1.0 is still the more widely-used version, this article describes XPath 1.0.
The XPath language is based on a tree representation of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria. In popular use (though not in the official specification), an XPath expression is often referred to simply as an XPath.
Originally motivated by a desire to provide a common syntax and behavior model between XPointer and XSLT, XPath has rapidly been adopted by developers as a small query language, and subsets are used in other W3C specifications such as XML Schema and XForms.
Using XPath sample
1. Create the DOM XML document
String fileName = "D:/sample.xml"
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new File(fileName));
2. Create XPATH query string like "/parent/child/grandchild'[@attribute = value]'"
for eg
String XpathQueryContexts = "/root/module[@display='" + moduleKey +
"']/subjectArea[@name='" + subjAreaKey +
"']/tab[@targettable='" + tabKey + "']/" +
"filter-contexts";
root is the parent node
subjectArea is the child node for root
tab is the child node for subjectArea
targettable is a property defined in the tab node
3. Get the nodes as a ArrayList for the given query condition at step 2
List lstContexts = XPath.selectNodes(jDomDocument, XpathQueryContexts);
More information available in
http://en.wikipedia.org/wiki/XPath
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment