API java : javax.xml.xpath


Package javax.xml.xpath

This package provides an object-model neutral API for the evaluation of XPath expressions and access to the evaluation environment.

See:
          Description

Interface Summary
XPath XPath provides access to the XPath evaluation environment and expressions.
XPathExpression XPathExpression provides access to compiled XPath expressions.
XPathFunction XPathFunction provides access to XPath functions.
XPathFunctionResolver XPathFunctionResolver provides access to the set of user defined XPathFunctions.
XPathVariableResolver XPathVariableResolver provides access to the set of user defined XPath variables.
 

Class Summary
XPathConstants XPath constants.
XPathFactory An XPathFactory instance can be used to create XPath objects.
 

Exception Summary
XPathException XPathException represents a generic XPath exception.
XPathExpressionException XPathExpressionException represents an error in an XPath expression.
XPathFactoryConfigurationException XPathFactoryConfigurationException represents a configuration error in a XPathFactory environment.
XPathFunctionException XPathFunctionException represents an error with an XPath function.
 

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 Overview

The 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 Expressions

An 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:

  1. /foo/bar

This example would select the <bar> element in an XML document such as the following:

  1. <foo>
  2. <bar/>
  3. </foo>

The expression /foo/bar is an example of a location path. While XPath location paths resemble Unix-style file system paths, an important distinction is that XPath expressions return all nodes that match the expression. Thus, all three <bar> elements in the following document would be selected by the /foo/bar expression:

  1. <foo>
  2. <bar/>
  3. <bar/>
  4. <bar/>
  5. </foo>

A special location path operator, //, selects nodes at any depth in an XML document. The following example selects all <bar> elements regardless of their location in a document:

  1. //bar

A wildcard operator, *, causes all element nodes to be selected. The following example selects all children elements of a <foo> element:

  1. /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:

Location Path Description
/foo/bar/@id Selects the attribute id of the <bar> element
/foo/bar/text() Selects the text nodes of the <bar> element. No distinction is made between escaped and non-escaped character data.
/foo/bar/comment() Selects all comment nodes contained in the <bar> element.
/foo/bar/processing-instruction() Selects all processing-instruction nodes contained in the <bar> element.

Predicates allow for refining the nodes selected by an XPath location path. Predicates are of the form [expression]. The following example selects all <foo> elements that contain an include attribute with the value of true:

  1. //foo[@include='true']

Predicates may be appended to each other to further refine an expression, such as:

  1. //foo[@include='true'][@mode='bar']

Using the XPath API

The following example demonstrates using the XPath API to select one or more nodes from an XML document:

  1. XPath xpath = XPathFactory.newInstance().newXPath();
  2. String expression = "/widgets/widget";
  3. InputSource inputSource = new InputSource("widgets.xml");
  4. NodeSet nodes = (NodeSet) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);

XPath Expressions and Types

While 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:

  • Boolean
  • Number
  • String

The desired return type is specified by a QName parameter in method call used to evaluate the expression, which is either a call to XPathExpression.evalute(...) or to one of the XPath.evaluate(...) convenience methods. The allowed QName values are specified as constants in the XPathConstants class; they are:

When a Boolean return type is requested, Boolean.TRUE is returned if one or more nodes were selected; otherwise, Boolean.FALSE is returned.

The String return type is a convenience for retrieving the character data from a text node, attribute node, comment node, or processing-instruction node. When used on an element node, the value of the child text nodes is returned.

The Number return type attempts to coalesce the text of a node to a double data type.

XPath Context

XPath location paths may be relative to a particular node in the document, known as the context. Consider the following XML document:

  1. <widgets>
  2. <widget>
  3. <manufacturer/>
  4. <dimensions/>
  5. </widget>
  6. </widgets>

The <widget> element can be selected with the following XPath API code:

  1. // parse the XML as a W3C Document
  2. DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  3. Document document = builder.parse(new File("/widgets.xml"));
  4.  
  5. XPath xpath = XPathFactory.newInstance().newXPath();
  6. String expression = "/widgets/widget";
  7. Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);

With a reference to the <widget> element, a relative XPath expression can now written to select the <manufacturer> child element:

  1. XPath xpath = XPathFactory.newInstance().newXPath();
  2. <strong>String expression = "manufacturer";</strong>
  3. Node manufacturerNode = (Node) xpath.evaluate(expression, <strong>widgetNode</strong>, XPathConstants.NODE);


Ces informations proviennent du site de http://java.sun.com

Remarques

Contenu

Le contenu de cette page provient du site de Sun, et est généré depuis un cache sur l'infobrol après certains traitements automatisés. La présentation peut donc différer du document original, mais le contenu aussi. Vous pouvez utiliser ce bouton pour afficher la page originale du site de Sun :

Quels sont les motivations de cette démarche?

Maintenir les pages en cache sur différents sites peut offrir plus de disponibilité.

Chaque page est indexée dans la base de donnée, ce qui permet de retrouver facilement les informations, au moyen des sommaires, du moteur de recherche interne, etc.

Des facilités sont mises en place pour que les membres de l'infobrol puissent effectuer des traductions en français des différents documents. Ceci devrait permettre aux débutants en programmation Java de consulter les API en français s'ils maîtrisent moins bien la langue de Shakespeare. Dans le cas où une traduction a été soumise, elle est disponible au moyen d'un lien en bas de page. Si la traduction a été validée, la page s'affiche par défaut en français, et un lien en bas de page permet d'atteindre la version en anglais.

Le code sur l'infobrol est automatiquement coloré selon la syntaxe, et les différents mots clés sont transformés en liens pour accéder rapidement aux informations.

Vous avez la possibilité de partager vos expériences en proposant vos propres extraits de code en utilisant le bouton "ajouter un commentaire" en bas de page. Si vous visitez simplement l'infobrol, vous avez déjà accès à cette fonction, mais si vous étes membre du brol, vous pouvez en plus utiliser des boutons supplémentaires de mise en forme, dont la coloration automatique de vos extraits de codes.

Réseaux sociaux

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher les interactions avec les réseaux sociaux sur ces pages.

 

Nuage de mots clés

7 mots clés dont 0 définis manuellement (plus d'information...).

Avertissement

Cette page ne possède pas encore de mots clés manuels, ceci est donc un exemple automatique (les niveaux de pertinence sont fictifs, mais les liens sont valables). Pour tester le nuage avec une page qui contient des mots définis manuellement, vous pouvez cliquer ici.

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher le nuage de mots clés.

 

Astuce pour imprimer les couleurs des cellules de tableaux : http://www.gaudry.be/ast-rf-450.html
Aucun commentaire pour cette page

© Ce document issu de l′infobrol est enregistré sous le certificat Cyber PrInterDeposit Digital Numbertection. Enregistrement IDDN n° 5329-1703
Document créé le 30/08/06 20:37, dernière modification le Vendredi 17 Juin 2011, 12:12
Source du document imprimé : http://www.gaudry.be/java-api-rf-javax/xml/xpath/package-summary.html Document affiché 1 fois ce mois de Juin.
St.Gaudry©07.01.02
 
l'infobrol
Nous sommes le Samedi 02 Juin 2012, 09:34, toutes les heures sont au format GMT+1.00 Heure, heure d'été (+1)