android XPath parsing xml

黄舟
Release: 2017-02-09 13:55:50
Original
2187 people have browsed it

XPath is a language for finding information in XML documents. XPath can be used to traverse elements and attributes in XML documents.

XPath is a major element of the W3C XSLT standard, and both XQuery and XPointer are built on top of XPath expressions.

Therefore, an understanding of XPath is the basis for many advanced XML applications. To learn more about XPath, please refer to http://www.w3school.com.cn/xpath/index.asp.

XPath is only suitable for querying xml information. It is not recommended to use this method for complete parsing of xml files. The best ways to parse xml files are sax and pull.

I did this test on Android 2.2 system. I don’t know if it will work if it is lower than 2.2.

Let’s talk about the steps of XPath parsing xml in detail: xpathTest.xml is the same as DomTest.xml in the android dom parsing xml method

1. Create InputSources

2. Get an XPathFactory instance.

3. Use the XPathFactory instance to get the XPath instance

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

private void xPathParserXml(){  
        //获取XPathFactory实例  
        XPathFactory factory = XPathFactory.newInstance();  
        //用工程生成XPath实例,解析xml  
        XPath xpath = factory.newXPath();  
        //  
        try {  
            InputSource source = new InputSource(getResources().getAssets().open("xPathTest.xml"));  
          
            //第一个参数:需要查询的节点名称,必须要在节点前加“//”  
            //第二个参数:查询的输入源  
            //第三个参数:返回的类型  
//          NodeList nodeList = (NodeList) xpath.evaluate("//group", source, XPathConstants.NODESET);  
//          if(nodeList != null && nodeList.getLength() > 0){  
//              for(int i = 0;i < nodeList.getLength();i++){  
//                  Node node = nodeList.item(i);  
//                  //在这也可以得到<group>的子节点<person>。但是这样不符合xpath的风格。  
//                  NodeList personList = node.getChildNodes();  
//                  Element  nodeAttr =(Element)node;  
//                  String groupName = nodeAttr.getAttribute("name");  
//                  String num = nodeAttr.getAttribute("num");  
//                    
//                  Log.e("TEST", ""+groupName+"   "+num);  
//              }  
//          }  
              
//          //获取<person>节点信息  
//          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);  
//                  //在这也可以得到<person>的子节点<chinese>和<english>。  
//                  NodeList childList = node.getChildNodes();  
//                  String groupName = node.getAttribute("name");  
//                  String age = node.getAttribute("age");  
//                    
//                  Log.e("TEST", ""+groupName+"   "+age);  
//              }  
//          }  
              
            //获取<chinese>节点信息  
            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();  
        }  
          
    }
Copy after login

Note: xpath.evaluate() cannot be called Twice, an error was reported. The reason is unclear. If you know the reason, please leave a message to let us know, thank you.

Some people have asked whether XPath can query large xml files (more than 1M or more). This should be possible in theory, as long as you can solve the problem that InputSource can read large-capacity files.

The above is the content of android XPath parsing xml. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template