search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Home Backend Development XML/RSS Tutorial android XPath parsing xml

android XPath parsing xml

Feb 17, 2017 pm 03:26 PM




##XPath is a A language for finding information in XML documents. XPath can be used to traverse elements and attributes in XML documents.

XPath is a major element of the W3C XSLT standard, and both XQuery and XPointer are built on top of XPath expressions.

Therefore, an understanding of XPath is the basis for many advanced XML applications. To learn more about XPath, please refer to //m.sbmmt.com/.

Two ways.

I did this test on the android 2.2 system. I don’t know if it will work if it is lower than 2.2.

Let’s talk about the steps of XPath parsing xml in detail: xpathTest.xml and

android The way dom parses xml is the same as DomTest.xml in

1. Create InputSources

2. Obtain the XPathFactory instance.

3. Use the XPathFactory instance to obtain the XPath instance

4. XPath calls the evaluate() method to obtain the queried 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();
		}
    	
    }


Note: xpath.evaluate() cannot be called twice and an error will be reported. The reason is unclear. If you know the reason, please leave a message to let us know, thank you.


Some people have asked whether XPath can query large xml files (more than 1M or more). This should be possible in theory, as long as you can Just solve the problem that InputSource can read large-capacity files.

The above is the content of android XPath parsing xml. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to change the song cache directory in NetEase Cloud Music How to change the song cache directory in NetEase Cloud Music Jan 13, 2026 am 10:30 AM

In the process of using NetEase Cloud Music, you occasionally need to adjust the song cache path, for example, if the original cache location is running out of disk space, or you want to store all cached files in a designated folder. The following will give you a clear explanation on how to modify the song cache path of NetEase Cloud Music. Launch the NetEase Cloud Music App on the Android system on the mobile phone. Click the "≡" three horizontal lines icon in the upper left corner to bring up the side menu bar. Swipe to find and click "Settings" in the sidebar. After entering the settings page, locate the "Storage Path" option. The cache folder currently displayed is the cache folder in use. Click "Storage Path", and the system will bring up the local file manager, allowing you to select any folder in the built-in storage of the fuselage, or a directory in the mounted SD card. select

Where to download the latest version of iMoutai? iMoutai official app safety installation guide Where to download the latest version of iMoutai? iMoutai official app safety installation guide Jan 13, 2026 pm 04:42 PM

The official download entrance for the latest version of iMoutai v1.9.0 is the official website https://www.moutaichina.com/, which supports dual-end downloads for Android and iOS. The installation package is transmitted through SSL encryption and SHA256 verification to ensure security and trustworthiness.

What to do if the text layout of the Quark browser webpage is confusing? Quark browser page adaptation tutorial What to do if the text layout of the Quark browser webpage is confusing? Quark browser page adaptation tutorial Jan 15, 2026 pm 02:24 PM

When Quark Browser's web page layout is disordered, it can be repaired by enabling "Web Page Adaptation Optimization", disabling hardware acceleration, clearing font and CSS cache, switching the UA logo, and turning off script interception.

Devin AI: Disrupting software engineering? An in-depth analysis of the future of AI software engineers Devin AI: Disrupting software engineering? An in-depth analysis of the future of AI software engineers Jan 12, 2026 pm 10:54 PM

In 2025, artificial intelligence (AI) is penetrating into various industries at an alarming rate, and the field of software engineering is no exception. Recently, an AI software engineer named DevinAI emerged, which has attracted widespread attention in the industry. DevinAI claims to be able to complete software development tasks autonomously, which has triggered concerns and discussions about whether AI will replace traditional software engineers. This article

The Ultimate Guide to AI Photo Editing: Create Social Media Hits Easily The Ultimate Guide to AI Photo Editing: Create Social Media Hits Easily Jan 08, 2026 pm 06:54 PM

In today's age of social media, a striking photo can often determine your online presence. Do you also want to have a photo that can quickly attract attention and attract attention? Now, AI technology makes this easier than ever. This blog will give you an in-depth understanding of how to use AI tools, especially Google Gemini AI, to easily edit photos and create stunning

What to do if the cache of Zeus Browser takes up too much? Clean the cache of Zeus Browser What to do if the cache of Zeus Browser takes up too much? Clean the cache of Zeus Browser Jan 10, 2026 pm 12:36 PM

Zeus Browser runs slowly, the page loads abnormally, or a large amount of storage space is occupied, which is probably caused by the backlog of cache files. It can be cleaned through five methods: the main interface shortcut entry, privacy settings, traffic consumption module, system-level shortcut keys, or manually deleting the local cache directory.

How to set the default QR code generation method in Win11_Win11 shared link automatic transcoding associated application [Tool] How to set the default QR code generation method in Win11_Win11 shared link automatic transcoding associated application [Tool] Jan 10, 2026 pm 01:33 PM

It is necessary to enable the system QR code function through ViVeTool, set the default sharing to Windows sharing, the WiFi password can directly generate the QR code in the WLAN settings, and fix icon abnormalities caused by high contrast or third-party startup items.

How to use AI to color black and white photos AI old photo coloring tutorial How to use AI to color black and white photos AI old photo coloring tutorial Jan 08, 2026 pm 12:51 PM

You can use the DeOldify online platform, Photoshop Beta version neural filter, Python to run the Colourful model locally, or the Remini mobile app to automatically color black and white old photos. Each method is suitable for ordinary users who do not need programming, professional designers, technical users who pay attention to privacy, and fast processing needs on the mobile terminal.

Related articles