android中XPath如何解析xml-创新互联

这篇文章将为大家详细讲解有关 android中XPath如何解析xml,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

坚守“ 做人真诚 · 做事靠谱 · 口碑至上 · 高效敬业 ”的价值观,专业网站建设服务10余年为成都护栏打桩机小微创业公司专业提供成都定制网站营销网站建设商城网站建设手机网站建设小程序网站建设网站改版,从内容策划、视觉设计、底层架构、网页布局、功能开发迭代于一体的高端网站建设服务。

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

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

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);  
//                  //在这也可以得到的子节点。但是这样不符合xpath的风格。  
//                  NodeList personList = node.getChildNodes();  
//                  Element  nodeAttr =(Element)node;  
//                  String groupName = nodeAttr.getAttribute("name");  
//                  String num = nodeAttr.getAttribute("num");  
//                    
//                  Log.e("TEST", ""+groupName+"   "+num);  
//              }  
//          }  
              
//          //获取节点信息  
//          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);  
//                  //在这也可以得到的子节点。  
//                  NodeList childList = node.getChildNodes();  
//                  String groupName = node.getAttribute("name");  
//                  String age = node.getAttribute("age");  
//                    
//                  Log.e("TEST", ""+groupName+"   "+age);  
//              }  
//          }  
              
            //获取节点信息  
            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”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:android中XPath如何解析xml-创新互联
标题来源:http://ybzwz.com/article/ejidg.html