>  기사  >  백엔드 개발  >  안드로이드 XPath 구문 분석 XML

안드로이드 XPath 구문 분석 XML

黄舟
黄舟원래의
2017-02-17 15:26:181902검색




XPath는 A 언어입니다. XML 문서에서 정보를 찾는 데 사용됩니다. XPath는 XML 문서의 요소와 속성을 탐색하는 데 사용할 수 있습니다.

XPath는 W3C XSLT 표준의 주요 요소이며 XQuery와 XPointer는 모두 XPath 표현식을 기반으로 구축되었습니다.

따라서 XPath에 대한 이해는 많은 고급 XML 애플리케이션의 기초입니다. XPath에 대한 자세한 내용은 http://www.php.cn/을 참조하세요.

XPath는 xml 정보를 쿼리하는 데에만 적합합니다. xml 파일을 완전히 구문 분석하는 데는 이 방법을 사용하지 않는 것이 좋습니다. 두 가지 방법으로.

안드로이드 2.2 시스템에서 테스트를 해봤는데 2.2 미만이면 잘 될지 모르겠네요.

XPath가 xml을 구문 분석하는 단계에 대해 자세히 설명하겠습니다: xpathTest.xml 및 android dom이 xml을 구문 분석하는 방식은

의 DomTest.xml과 동일합니다. 1. 입력 소스 생성

2. XPathFactory 인스턴스를 얻습니다.

3. XPathFactory 인스턴스를 사용하여 XPath 인스턴스를 얻습니다.

XPath는 평가() 메서드를 호출하여 쿼리된 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 parsing xml 내용입니다. 더 많은 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 참고해주세요!



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.