android中XPath如何解析xml-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關 android中XPath如何解析xml,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

堅守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價值觀,專業(yè)網(wǎng)站建設服務10余年為成都護欄打樁機小微創(chuàng)業(yè)公司專業(yè)提供成都定制網(wǎng)站營銷網(wǎng)站建設商城網(wǎng)站建設手機網(wǎng)站建設小程序網(wǎng)站建設網(wǎng)站改版,從內(nèi)容策劃、視覺設計、底層架構、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設服務。

XPath 是一門在 XML 文檔中查找信息的語言。XPath 可用來在 XML 文檔中對元素和屬性進行遍歷。

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

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

我是在Android 2.2系統(tǒng)上做的這個測試,低于2.2不知道行不行。

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

1、創(chuàng)建InputSources

2、獲得XPathFactory實例。

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

4、XPath調(diào)用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"));  
          
            //第一個參數(shù):需要查詢的節(jié)點名稱,必須要在節(jié)點前加“//”  
            //第二個參數(shù):查詢的輸入源  
            //第三個參數(shù):返回的類型  
//          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>的子節(jié)點<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>節(jié)點信息  
//          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>的子節(jié)點<chinese>和<english>。  
//                  NodeList childList = node.getChildNodes();  
//                  String groupName = node.getAttribute("name");  
//                  String age = node.getAttribute("age");  
//                    
//                  Log.e("TEST", ""+groupName+"   "+age);  
//              }  
//          }  
              
            //獲取<chinese>節(jié)點信息  
            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()不能調(diào)用兩次,報錯誤。至于原因不清楚。知道原因的請留言告知,謝謝。

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

關于“ android中XPath如何解析xml”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網(wǎng)頁標題:android中XPath如何解析xml-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://muchs.cn/article16/ejidg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機、標簽優(yōu)化、網(wǎng)站內(nèi)鏈、網(wǎng)站制作、品牌網(wǎng)站建設、服務器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司