The XML-based tree structure of XPath provides the ability to search for nodes in the data structure tree. The following is a small example, if you need it, you can refer to XPath, which is the XML Path Language. it is a Language used to determine a part of the XML document. The XML-based tree structure of XPath provides the ability to search nodes in the data structure tree. At first, the original intention of XPath was to use it as a general syntax model between XPointer and XSL. However, XPath is quickly adopted by developers as a small query language.
XPathTest. java
Package 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; import org. w3c. dom. document; import org. w3c. dom. node; import org. w3c. dom. nodeList; import org. xml. sax. SAXException; public class XPathTest {public static void main (String [] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {// Parse the file and generate the document Object DocumentBuilder builder = DocumentBuilderFactory. newInstance (). newDocumentBuilder (); Document document = builder. parse (new File ("bookstore. xml "); // Generate the XPath object XPath xpath = XPathFactory. newInstance (). newXPath (); // Obtain the node value String webTitle = (String) xpath. evaluate ("/bookstore/book [@ category = 'web']/title/text ()", document, XPathConstants. STRING); System. out. println (webTitle); System. out. println ("========================================== ============================== "); // Obtain the node attribute value String webTitleLang = (String) xpath. evaluate ("/bookstore/book [@ category = 'web']/title/@ lang", document, XPathConstants. STRING); System. out. println (webTitleLang); System. out. println ("========================================== ============================== "); // Obtain the Node object Node bookWeb = (Node) xpath. evaluate ("/bookstore/book [@ category = 'web']", document, XPathConstants. NODE); System. out. println (bookWeb. getNodeName (); System. out. println ("========================================== ============================== "); // Obtain the node set NodeList books = (NodeList) xpath. evaluate ("/bookstore/book", document, XPathConstants. NODESET); for (int I = 0; I <books. getLength (); I ++) {Node book = books. item (I); System. out. println (xpath. evaluate ("@ category", book, XPathConstants. STRING);} System. out. println ("========================================== ================================== ");}}
Bookstore. xml
Everyday Italian Giada De Laurentiis
2005
30.00
Harry Potter J K. Rowling
2005
29.99
Learning XML Erik T. Ray
2003
39.95
For more articles about how to use xpath to parse xml examples in java, refer to the Chinese PHP website!