WebLogic’s XPath API
WebLogic XPath API lets you match XPath
expressions against an XML document, represented either as a DOM tree
or an XMLInputStream. The API is contained in the
weblogic.xml.xpath.* package. Though the XPath
syntax and semantics follow the W3C standard, there is still no
standard Java API for manipulating XPath expressions, so this is a
welcome API.
Using the API with a DOM
To
use the API, construct DOMXPath instances
representing your XPath. Here are a few examples:
DOMXPath threeDebt = new DOMXPath("sum(//co[@type='3']/debt)");
DOMXPath haveFirst = new DOMXPath("count(//co[@type='1']) > 0");
DOMXPath biggestDebt = new DOMXPath("//co[debt > 1000]/debt");We will use the following XML as test input:
<world> <co type='1'><debt>100</debt></co> <co type='3'><debt>1000</debt></co> <co type='3'><debt>2000</debt></co> </world>
You can match the XPath expressions against either a DOM node or the
entire document itself. Use a node if you intend to just search a
portion of the document. To evaluate the XPath expression, you must
invoke one of the following evaluate methods on the
DOMXPath instance:
public boolean evaluateAsBoolean(Document|Node); public java.util.Set evaluateAsNodeset(Document|Node); public double evaluateAsNumber(Document|Node); public String evaluateAsString(Document|Node);
The actual evaluate method you invoke will depend on the type of the expected result(s) of your XPath expression. For example, we would invoke the earlier XPath expressions as follows: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access