Java Reference
In-Depth Information
System.err.println("FCE: "+fce);
}
catch (ParserConfigurationException pce)
{
System.err.println("PCE: "+pce);
}
catch (XPathException xpe)
{
System.err.println("XPE: "+xpe);
}
}
}
After parsing contacts.xml and building the DOM tree, main() instantiates
XPathFactory by calling its XPathFactory newInstance() method. The
resulting XPathFactory instance can be used to set features (such as secure pro-
cessing, to process XML documents securely) by calling its void
setFeature(String name, boolean value) method,createan XPath ob-
ject by calling its XPath newXPath() method, and more.
XPath declares an XPathExpression compile(String expression)
methodforcompilingthespecified expression (anXPathexpression)andreturning
thecompiledexpressionasaninstanceofaclassthatimplementsthe XPathExpres-
sion interface.Thismethodthrows XPathExpressionException (asubclassof
XMLException ) when the expression cannot be compiled.
XPath also declares several overloaded evaluate() methods for immediately
evaluatinganexpressionandreturningtheresult.Becauseitcantaketimetoevaluatean
expression,youmightchoosetocompileacomplexexpressionfirst(toboostperform-
ance) when you plan to evaluate this expression many times.
After compiling the expression, main() calls XPathExpression 's Object
evaluate(Object item, QName returnType) method to evaluate the ex-
pression.Thefirstargumentisthecontextnodefortheexpression,whichhappenstobe
a Document instanceintheexample.Thesecondargumentspecifiesthekindofobject
returned by evaluate() and is set to XPathConstants.NODESET , a qualified
namefortheXPath1.0nodesettype,whichisimplementedviaDOM's NodeList in-
terface.
Search WWH ::




Custom Search