XPATH es el lenguaje de ruta XML, que es un lenguaje utilizado para determinar una determinada parte del documento XML. XPATH se basa en la estructura de árboles tipo XML, proporcionando la capacidad de encontrar nodos en el árbol de estructura de datos. Al principio, la intención original de XPath era usarlo como un modelo de sintaxis general entre XPointer y XSL. Pero Xpath fue rápidamente adoptado por los desarrolladores como un pequeño lenguaje de consulta.
Xpathtest.java
La copia del código es la siguiente:
paquete com.hongyuan.test;
import java.io.file;
import java.io.ioException;
import javax.xml.parsers.documentBuilder;
import javax.xml.parsers.documentBuilderFactory;
import javax.xml.parsers.parserConfigurationException;
import javax.xml.xpath.xpath;
import javax.xml.xpath.xpathconstants;
import javax.xml.xpath.xpathexpressionException;
import javax.xml.xpath.xpathFactory;
importar org.w3c.dom.document;
importar org.w3c.dom.node;
importar org.w3c.dom.nodelist;
importar org.xml.sax.saxException;
clase pública XPATHTEST {
Public static void main (string [] args) lanza ParserconfigurationException,
SaxException, ioexception, xpathexpressionException {
// analizar el archivo y generar el objeto de documento
DocumentBuilder Builder = DocumentBuilderFactory.NewinStance ()
.NewDocumentBuilder ();
Documento documento = builder.parse (nuevo archivo ("bookstore.xml"));
// Generar el objeto XPath
XPATH XPATH = XPATHFACTORY.NEWINSTANCE (). NewxPath ();
// Obtener el valor del nodo
String webtitle = (string) xpath.evaluate (
"/bookstore/book [@category = 'web']/title/text ()", documento,
Xpathconstants.string);
System.out.println (Webtitle);
System.out.println ("============================================== ================== ================================== ==================================================== ==============================================
// Obtener el valor del atributo de nodo
String webtitlelang = (string) xpath.evaluate (
"/bookstore/book [@category = 'web']/title/@lang", documento,
Xpathconstants.string);
System.out.println (webTitLelang);
System.out.println ("============================================== ================== ================================== ==================================================== ==============================================
// Obtener el objeto de nodo
Nodo bookweb = (nodo) xpath.evaluate (
"/bookstore/book [@category = 'web']", documento,
Xpathconstants.node);
System.out.println (bookWeb.getNodeName ());
System.out.println ("============================================== ================== ================================== ==================================================== ==============================================
// Obtener la colección de nodos
Nodelist books = (nodelist) xpath.evaluate ("/bookstore/book", documento,
Xpathconstants.nodeset);
para (int i = 0; i <books.getLength (); i ++) {
Nodo Libro = Books.Item (i);
System.out.println (xpath.evaluate ("@categoría", libro,
Xpathconstants.string));
}
System.out.println ("============================================== ================== ================================== ==================================================== ==============================================
}
}
bookstore.xml
La copia del código es la siguiente:
<? xml versión = "1.0" encoding = "utf-8"?>
<Bookstore>
<Libro categoría = "Cooking">
<title lang = "en"> Everyday Italian </title>
<HauT> GIADA DE LAURENTIIS </Author>
<Year> 2005 </año>
<Precio> 30.00 </Price>
</libro>
<Libro categoría = "Niños">
<title lang = "en"> Harry Potter </title>
<HauT> J K. Rowling </Author>
<Year> 2005 </año>
<Price> 29.99 </Price>
</libro>
<Libro categoría = "web">
<title lang = "en"> Learning XML </title>
<HauT> Erik T. Ray </Author>
<nemio> 2003 </año>
<precio> 39.95 </pree>
</libro>
</bookstore>
Efecto de ejecución