溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

android中如何利用XPath 解析xml

發(fā)布時(shí)間:2021-07-12 13:45:43 來(lái)源:億速云 閱讀:189 作者:Leah 欄目:編程語(yǔ)言

今天就跟大家聊聊有關(guān)android中如何利用XPath 解析xml,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

XPath 是一門(mén)在 XML 文檔中查找信息的語(yǔ)言。XPath 可用來(lái)在 XML 文檔中對(duì)元素和屬性進(jìn)行遍歷。

XPath 是 W3C XSLT 標(biāo)準(zhǔn)的主要元素,并且 XQuery 和 XPointer 同時(shí)被構(gòu)建于 XPath 表達(dá)之上。

因此,對(duì) XPath 的理解是很多高級(jí) XML 應(yīng)用的基礎(chǔ)。具體學(xué)習(xí)XPath參照http://www.php.cn/。

XPath只適合用來(lái)查詢(xún)xml的信息,對(duì)于完整的解析xml文件的建議不要使用這個(gè)方式,最好的解析xml文件應(yīng)該是sax,pull這兩種方式。

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

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

1、創(chuàng)建InputSources

2、獲得XPathFactory實(shí)例。

3、用XPathFactory實(shí)例獲取XPath的實(shí)例

4、XPath調(diào)用evaluate()方法獲取查詢(xún)出的NodeList

 private void xPathParserXml(){
    	//獲取XPathFactory實(shí)例
    	XPathFactory factory = XPathFactory.newInstance();
    	//用工程生成XPath實(shí)例,解析xml
    	XPath xpath = factory.newXPath();
    	//
		try {
			InputSource source = new InputSource(getResources().getAssets().open("xPathTest.xml"));
	    
			//第一個(gè)參數(shù):需要查詢(xún)的節(jié)點(diǎn)名稱(chēng),必須要在節(jié)點(diǎn)前加“//”
	    	//第二個(gè)參數(shù):查詢(xún)的輸入源
	    	//第三個(gè)參數(shù):返回的類(lèi)型
//	    	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é)點(diǎn)<person>。但是這樣不符合x(chóng)path的風(fēng)格。
//	    			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é)點(diǎn)信息
//	    	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é)點(diǎn)<chinese>和<english>。
//	    			NodeList childList = node.getChildNodes();
//	    			String groupName = node.getAttribute("name");
//	    			String age = node.getAttribute("age");
//	    			
//	    			Log.e("TEST", ""+groupName+"   "+age);
//	    		}
//	    	}
	    	
	    	//獲取<chinese>節(jié)點(diǎn)信息
			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)用兩次,報(bào)錯(cuò)誤。至于原因不清楚。知道原因的請(qǐng)留言告知,謝謝。

對(duì)已有人提出XPath能不能查詢(xún)很大的xml文件(超過(guò)1M或),這個(gè)在理論上應(yīng)該可以,只要你能解決InputSource可以讀取大容量文件問(wèn)題就可以了。

看完上述內(nèi)容,你們對(duì)android中如何利用XPath 解析xml有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI