|
Préférences
Moteurs de recherche
|
|||||||||||||||||||||||||||||||||||||
JavaTM 2 Platform Std. Ed. v1.6.0
Package javax.xml.xpathThis package provides an object-model neutral API for the evaluation of XPath expressions and access to the evaluation environment.
See:
Package javax.xml.xpath Description
This package provides an object-model neutral API for the evaluation of XPath expressions and access to the evaluation environment. The following XML standards apply: XPath OverviewThe XPath language provides a simple, concise syntax for selecting nodes from an XML document. XPath also provides rules for converting a node in an XML document object model (DOM) tree to a boolean, double, or string value. XPath is a W3C-defined language and an official W3C recommendation; the W3C hosts the XML Path Language (XPath) Version 1.0 specification. XPath started in life in 1999 as a supplement to the XSLT and XPointer languages, but has more recently become popular as a stand-alone language, as a single XPath expression can be used to replace many lines of DOM API code. XPath ExpressionsAn XPath expression is composed of a location path and one or more optional predicates. Expressions may also include XPath variables. The following is an example of a simple XPath expression: /foo/bar This example would select the <foo> <bar/> </foo> The expression <foo> <bar/> <bar/> <bar/> </foo> A special location path operator, //bar A wildcard operator, *, causes all element nodes to be selected.
The following example selects all children elements of a
/foo/* In addition to element nodes, XPath location paths may also address attribute nodes, text nodes, comment nodes, and processing instruction nodes. The following table gives examples of location paths for each of these node types:
Predicates allow for refining the nodes selected by an XPath
location path. Predicates are of the form
//foo[@include='true'] Predicates may be appended to each other to further refine an expression, such as: //foo[@include='true'][@mode='bar'] Using the XPath APIThe following example demonstrates using the XPath API to select one or more nodes from an XML document: XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; InputSource inputSource = new InputSource("widgets.xml"); NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); XPath Expressions and TypesWhile XPath expressions select nodes in the XML document, the XPath API allows the selected nodes to be coalesced into one of the following other data types:
The desired return type is specified by a
When a The The XPath ContextXPath location paths may be relative to a particular node in the
document, known as the <widgets> <widget> <manufacturer/> <dimensions/> </widget> </widgets> The // parse the XML as a W3C Document DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(new File("/widgets.xml")); XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE); With a reference to the XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "manufacturer"; Node manufacturerNode = (Node) xpath.evaluate(expression, widgetNode, XPathConstants.NODE);
Copyright 2003 Sun Microsystems, Inc. All rights reserved
|