首頁  >  文章  >  後端開發  >  android XPath 解析xml

android XPath 解析xml

黄舟
黄舟原創
2017-02-09 13:55:502146瀏覽

XPath 是一門在 XML 文件中尋找資訊的語言。 XPath 可用於在 XML 文件中對元素和屬性進行遍歷。

XPath 是 W3C XSLT 標準的主要元素,並且 XQuery 和 XPointer 同時被建構於 XPath 表達之上。

因此,對 XPath 的理解是許多進階 XML 應用的基礎。具體學習XPath參考http://www.w3school.com.cn/xpath/index.asp。

XPath只適合用來查詢xml的信息,對於完整的解析xml文件的建議不要使用這個方式,最好的解析xml文件應該是sax,pull這兩種方式。

我是在Android 2.2系統上做的這個測試,低於2.2不知道行不行。

下面就具體說下XPath解析xml的步驟:xpathTest.xml 和android dom 解析xml方式 中的DomTest.xml一樣

1、創建InputSources

2、獲得XPathFactory實例。

3、用XPathFactory實例取得XPath的實例

4、XPath呼叫evaluate()方法取得查詢出的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();  
        }  
          
    }

注意:xpath.evaluate()不能呼叫兩次,錯誤。至於原因不清楚。知道原因的請留言告知,謝謝。

對已有人提出XPath能不能查詢很大的xml檔(超過1M或),這個在理論上應該可以,只要你能解決InputSource可以讀取大容量文件問題就可以了。

以上就是 android XPath 解析xml的內容,更多相關內容請關注PHP中文網(www.php.cn)!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn