Using xpath to parse xml examples in java

Source: Internet
Author: User
Tags php website
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.