XPATH est le langage du chemin XML, qui est une langue utilisée pour déterminer une certaine partie du document XML. XPATH est basé sur la structure des arbres de type XML, offrant la possibilité de trouver des nœuds dans l'arborescence de la structure des données. Au début, l'intention d'origine de XPATH était de l'utiliser comme modèle de syntaxe générale entre Xpointer et XSL. Mais XPath a été rapidement adopté par les développeurs comme un petit langage de requête.
Xpathtest.java
La copie de code est la suivante:
Package com.hongyuan.test;
Importer java.io.file;
Importer java.io.ioException;
Importer javax.xml.parsers.DocumentBuilder;
Importer javax.xml.parsers.DocumentBuilderFactory;
Importer javax.xml.parsers.PaSerConfigurationException;
Importer javax.xml.xpath.xpath;
Importer javax.xml.xpath.xpathConstants;
Importer javax.xml.xpath.xpathexpressionException;
Importer javax.xml.xpath.xpathfactory;
import org.w3c.dom.Document;
import org.w3c.dom.node;
import org.w3c.dom.nodelist;
import org.xml.sax.saxException;
classe publique XPathTest {
public static void main (String [] args) lève ParserConfigurationException,
SaxException, ioException, xpathexpressionException {
// analyser le fichier et générer l'objet document
DocumentBuilder Builder = DocumentBuilderFactory.NewInstance ()
.newDocumentBuilder ();
Document document = builder.parse (nouveau fichier ("Bookstore.xml"));
// Générer l'objet XPATH
Xpath xpath = xpathfactory.newinstance (). NewXPath ();
// Obtenez la valeur du nœud
String webTitle = (String) xpath.evaluate (
"/ librairie / livre [@ catégories = 'web'] / title / text ()", document,
XpathConstants.String);
System.out.println (WebTitle);
System.out.println ("=========================================================. ================== =============================================. ==============================================. ==========================================.
// Obtenez la valeur d'attribut de nœud
Chaîne webtitlelang = (string) xpath.evaluate (
"/ librairie / livre [@ catégories = 'web'] / title / @ lang", document,
XpathConstants.String);
System.out.println (WebTitlelang);
System.out.println ("=========================================================. ================== =============================================. ==============================================. ==========================================.
// Obtenez l'objet nœud
Node bookweb = (node) xpath.evaluate (
"/ librairie / livre [@ catégories = 'web']", document,
XpathConstants.Node);
System.out.println (bookweb.getNodeName ());
System.out.println ("=========================================================. ================== =============================================. ==============================================. ==========================================.
// Obtenez la collection de nœuds
Nodelist Books = (NodeList) xpath.evaluate ("/ librairie / livre", document,
XpathConstants.NoDeset);
pour (int i = 0; i <books.getLength (); i ++) {
Node book = books.item (i);
System.out.println (xpath.evaluate ("@ catégorie", livre,
XpathConstants.String));
}
System.out.println ("=========================================================. ================== =============================================. ==============================================. ==========================================.
}
}
librairie.xml
La copie de code est la suivante:
<? xml version = "1.0" Encoding = "utf-8"?>
<bookstore>
<book category = "Cooking">
<title lang = "en"> italien de tous les jours </Title>
<Author> Giada de Laurentiis </auteur>
<Near> 2005 </non>
<Prix> 30.00 </CI
</book>
<book category = "enfants">
<title Lang = "en"> Harry Potter </Title>
<auteur> J K. Rowling </auteur>
<Near> 2005 </non>
<Prix> 29,99 </ Price>
</book>
<book category = "web">
<title lang = "en"> apprentissage xml </Title>
<auteur> Erik T. Ray </auteur>
<Near> 2003 </nof>
<prix> 39,95 </ prix>
</book>
</bookstore>
Effet de course