XPath 是一门在 XML 文档中查找信息的语言。XPath 可用来在 XML 文档中对元素和属性进行遍历。

XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointer 同时被构建于 XPath 表达之上。

因此,对 XPath 的理解是很多高级 XML 应用的基础。具体学习XPath参照http://www.php.cn/。

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核实处理。

  • 相关标签:android,XPath,xml
  • 程序员必备接口测试调试工具:点击使用

    Apipost = Postman + Swagger + Mock + Jmeter

    Api设计、调试、文档、自动化测试工具

    网页生成APP,用做网站的技术去做APP:立即创建

    手机网站开发APP、自助封装APP、200+原生模块、2000+映射JS接口按需打包

    • 上一篇:小心XmlPullParser.netText()方法
    • 下一篇:java操作properties配置文件

    相关文章

    相关视频


    • 使用xmlhttp为网站增加域名查询功能的示例代码...
    • 四种XML解析方式详解
    • 基于PHP对XML的操作详解
    • XML和Tomcat的入门知识的详细介绍
    • android XPath 解析xml
    • Vue3 事件修饰符
    • vue3 指令
    • vue3 基础语法
    • vue3 组合api和选项api介绍

    视频教程分类

    • php视频教程
    • html视频教程
    • css视频教程
    • JS视频教程
    • jQuery视频教程
    • mysql视频教程
    • Linux视频教程
    • Python视频教程
    • Laravel视频教程
    • Vue视频教程

    专题

    android XPath 解析xml