Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
This is
an alternative for the old XPathAPI class, which provided static methods for
the purpose but had the drawback of instantiating a new XPathContext (and thus
building a new DTMManager, and new DTMs) each time it was called.
XPathAPIObject instead retains its context as long as the object persists,
reusing the DTMs. This does have a downside: if you've changed your source
document, you should obtain a new XPathAPIObject to continue searching it, since
trying to use the old DTMs will probably yield bad results or malfunction
outright... and the cached DTMs may consume memory until this object and its
context are returned to the heap. Essentially, it's the caller's responsibility
to decide when to discard the cache.
Method
Overviews
The
methods in this class are convenience methods into the low-level XPath API.
These functions tend to be a little slow, since a number of objects must be
created for each evaluation.
When
performance is a issue, then better to use precompile the XPaths using the
low-level API, and then just use the XPaths over and over.
Example of eval(Node contextNode, java.lang.String str)
eval() is used to used
to get an instance of XObject class.
Using XObject instance, you can get result nodes in the form of iterator or
NodeList
XObject xObject =
cachedXPathAPI.eval(doc,"//person"); //Here XPath “//person” is
applied on the context node doc.
Example of selectSingleNode(Node contextNode,
java.lang.String str)
selectSingleNode() is used to get
a Node by applying XPath on the context node. If the given xpath returns more
than one node, then only the first node will be returned.
Node node =
cachedXPathAPI.selectSingleNode(doc,"//person"); //The xpath
“//person” in applied on the context node