Parsing xml using androidXPath

Source: Internet
Author: User
Tags xml xpath
XPath is a language used to search for information in XML documents. XPath can be used to traverse elements and attributes in XML documents. XPath is a language used to search for information in XML documents. XPath can be used to traverse elements and attributes in XML documents.

XPath is the main element of W3C XSLT standards, and XQuery and XPointer are also built on XPath expressions.

Therefore, understanding XPath is the basis of many advanced XML applications. For more information about XPath, see http://www.w3school.com.cn/xpath/index.asp.

XPath is only suitable for querying xml Information. we recommend that you do not use this method for complete parsing of xml files. the best method for parsing xml files is sax and pull.

I did this test on the Android 2.2 System. I don't know if it is lower than 2.2.

The steps for parsing xml by XPath are as follows: xpathTest. xml is the same as DomTest. xml in xml parsing by android dom.

1. create InputSources

2. obtain the XPathFactory instance.

3. use an XPathFactory instance to obtain an XPath instance

4. XPath calls the evaluate () method to obtain the queried NodeList.

Private void xPathParserXml () {// Obtain XPathFactory instance XPathFactory = XPathFactory. newInstance (); // use a project to generate an XPath instance and parse xml XPath = factory. newXPath (); // try {InputSource source = new InputSource (getResources (). getAssets (). open ("xPathTest. xml "); // The first parameter: name of the node to be queried. you must add" // "before the node // The second parameter: input source // The third parameter returned // NodeList nodeList = (NodeList) xpath. evaluate ("// group", source, XPathConstants. NODES ET); // if (nodeList! = Null & nodeList. getLength ()> 0) {// for (int I = 0; I <nodeList. getLength (); I ++) {// Node node = nodeList. item (I); // You can also obtain
 
  
Subnode
  
   
. But this does not conform to the xpath style. // NodeList personList = node. getChildNodes (); // Element nodeAttr = (Element) node; // String groupName = nodeAttr. getAttribute ("name"); // String num = nodeAttr. getAttribute ("num"); // Log. e ("TEST", "" + groupName + "" + num); //} // Get
   
    
Node information // NodeList personList = (NodeList) xpath. evaluate ("// person", source, XPathConstants. NODESET); // if (personList! = Null & personList. getLength ()> 0) {// for (int I = 0; I <personList. getLength (); I ++) {// Element node = (Element) personList. item (I); // You can also obtain
    
     
Subnode
     
      
And
      
        . // NodeList childList = node. getChildNodes (); // String groupName = node. getAttribute ("name"); // String age = node. getAttribute ("age"); // Log. e ("TEST", "" + groupName + "" + age); //} // Obtain
       
         Node Information: NodeList chineseList = (NodeList) xpath. evaluate ("// chinese", source, XPathConstants. NODESET); if (chineseList! = Null & chineseList. getLength ()> 0) {for (int I = 0; I <chineseList. getLength (); I ++) {Node node = chineseList. item (I); String chinese = node. getTextContent (); Log. e ("TEST", "" + chinese) ;}} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (XPathExpressionException e) {e. printStackTrace ();}}
       
      
     
    
   
  
 

Note: xpath. evaluate () cannot be called twice and an error is returned. The reason is unclear. If you know the reason, please leave a message. thank you.

Whether or not XPath can be used to query a large xml file (more than 1 MB or more) should be feasible theoretically, as long as you can solve the problem that InputSource can read large-capacity files.

The above is the xml parsing content of android XPath. For more information, see PHP Chinese website (www.php1.cn )!

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.