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 pull parsing xml method

Android pull parsing xml method

Feb 17, 2017 pm 03:17 PM

 pull解析xml文件,和sax和dom一样 都可以脱离android单独使用,pull和sax的原理一样,不一样的地方是pull读取xml文件后调用方法返回的是数字, 
   读取到xml的声明返回数字0 START_DOCUMENT; 
   读取到xml的结束返回数字1 END_DOCUMENT ; 
   读取到xml的开始标签返回数字2 START_TAG 
   读取到xml的结束标签返回数字3 END_TAG 
   读取到xml的文本返回数字4 TEXT 

   pull是开源的项目 源码下载地址//m.sbmmt.com/ 

被解析的xml文档和android dom 解析xml方式 中的xml文档一样,命名为pullTest.xml.

private String pullParseXml(InputStream inputStream) {
		String result = "";
		//解析全部的xml
		boolean isParse = true;
		try {
			// 创建一个xmlPullParser的工厂
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			// 获取一个解析实例
			XmlPullParser parse = factory.newPullParser();
			// 设置输入流的编码格式
			parse.setInput(inputStream, "UTF-8");
			// 当前事件的类型
			int eventType = parse.getEventType();
			while (XmlPullParser.END_DOCUMENT != eventType) {
				// 当前节点的名称
				String nodeName = parse.getName();
				switch (eventType) {
				case XmlPullParser.START_TAG:
					if ("group".equals(nodeName)) {
						// 解析<group>节点中的属性值,getAttributeCount()获取属性的个数
						for (int i = 0; i < parse.getAttributeCount(); i++) {
							// 属性名称
							String groupName = parse.getAttributeName(i);
							// 属性名称对应的值
							String nameValue = parse.getAttributeValue(i);
							result = result + groupName + " = " + nameValue;
						}
						result += "\n";
					} else if ("person".equals(nodeName)) {
						String personName = parse.getAttributeValue(0);
						String age = parse.getAttributeValue(1);
						result = result + "personName = " + personName
								+ "age =" + age + "\n";
					} else if ("chinese".equals(nodeName)) {
						//节点对应的文本
						String chinese = parse.nextText();
						Pattern p = Pattern.compile("\\s*|\t|\r|\n");
						Matcher m = p.matcher(chinese);
						chinese = m.replaceAll("");

						result = result + "chinese = " + chinese;
					} else if ("english".equals(nodeName)) {
						String english = parse.nextText();

						Pattern p = Pattern.compile("\\s*|\t|\r|\n");
						Matcher m = p.matcher(english);
						english = m.replaceAll("");

						result = result + "english = " + english + "\n";
					}
					break;
				case XmlPullParser.END_TAG:
					//在解析到一个group节点完成时,退出解析xml文件
//					if("group".equals(nodeName)){
//						eventType = XmlPullParser.END_DOCUMENT; 
//						isParse = false;
//					}
					break;
				default:
					break;
				}
				//整个xml文件全部解析
				if(isParse){
					eventType = parse.next();
				}
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}

pull和sax不同最主要的体现在pull可以由客户随时终止解析xml.sax解析,只能从文档头一直读到尾,中间不能停止也不能对文件进行修改。直到解析完了整个文档才会返回。

//在解析到一个group节点完成时,退出解析xml文件
//					if("group".equals(nodeName)){
//						eventType = XmlPullParser.END_DOCUMENT; 
//						isParse = false;
//					}



只要满足退出解析的条件,只需要设置如下代码即可。

eventType = XmlPullParser.END_DOCUMENT;


pull解析方式用到的方法,大部分我都在代码中注释了。


 以上就是android  pull 解析xml方式 的内容,更多相关内容请关注PHP中文网(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)

Tencent Yuanbao official website latest login entrance Tencent Yuanbao AI usage address Tencent Yuanbao official website latest login entrance Tencent Yuanbao AI usage address Feb 04, 2026 pm 06:06 PM

The latest login portal of Tencent Yuanbao official website is https://yuanbao.tencent.com/, which is a direct web portal deployed by Tencent. It supports mainstream browsers without installation and has features such as lightweight loading, multi-round conversation memory, and barrier-free access.

What is a 'hackathon” champion in the cryptocurrency industry? What do they gain? What is a 'hackathon” champion in the cryptocurrency industry? What do they gain? Jan 31, 2026 pm 02:18 PM

The champion of the currency circle's "Hackerathon" is the one with the highest comprehensive score through on-chain testing, K verification, and anonymous review. He or she will enjoy cash rewards (such as $75,000 Grant), token incentives (such as 75,000 flash tokens), and ecological rights (such as conference speeches, protocol access, and application listings).

How to combine HTML5 password with fingerprint verification_Integrate biometrics to provide a convenient method [Method] How to combine HTML5 password with fingerprint verification_Integrate biometrics to provide a convenient method [Method] Feb 07, 2026 am 09:10 AM

HTML5 does not support direct calls to the fingerprint sensor, and biometric login must be implemented in an HTTPS environment through WebAuthenticationAPI (navigator.credentials); it is only a pure front-end control and does not have access to the hardware. The browser sandbox mechanism prohibits scripts from reading fingerprints or triggering the sensor.

How to enable Android subsystem developer mode in Win11_Windows11 WSA settings debugging How to enable Android subsystem developer mode in Win11_Windows11 WSA settings debugging Jan 26, 2026 pm 12:24 PM

If you have turned on the WSA developer mode, you can install the APK through ADB. The operations include: 1. Turn on the developer mode in WSA "Advanced Settings" and confirm the IP and port; 2. If the switch is gray, use PowerShell to execute the Reset-AppxPackage and adb restart commands; 3. Use adbconnect to verify whether the connection is successful.

How to accurately pass and summarize multi-level product prices in nested RecyclerView How to accurately pass and summarize multi-level product prices in nested RecyclerView Feb 06, 2026 pm 08:54 PM

This article explains in detail how to correctly calculate and return the total price of each store (including the main price of the product, the price and quantity of add-ons) in the three-layer nested RecyclerView (store → product → add-on) structure to avoid the problem of value loss caused by the Adapter life cycle and data binding timing.

How to turn on low blue light mode in Thunderbird AirPlus_Turn on eye protection mode in Thunderbird AirPlus [Protection] How to turn on low blue light mode in Thunderbird AirPlus_Turn on eye protection mode in Thunderbird AirPlus [Protection] Feb 07, 2026 pm 07:16 PM

Thunderbird AirPlus can turn on the low blue light mode in four ways: 1. Press and hold the right dial on the temple for 2 seconds to quickly start and stop; 2. Adjust the "Display and Brightness → Eye Protection Mode" in the magic box settings; 3. ThunderbirdVisionApp remote synchronization configuration; 4. HDMI source device linkage to trigger hardware-level protection.

What is SAX parsing XML? The difference between SAX and DOM What is SAX parsing XML? The difference between SAX and DOM Feb 05, 2026 am 06:54 AM

SAX parsing is an event-driven streaming XML processing method that triggers callbacks while reading. It has constant memory usage and is suitable for large files or memory-limited scenarios. DOM loads XML as a memory tree, which supports random access but consumes large memory.

Application of html statements_Comparison of loading speed of html statements on different devices [Introduction] Application of html statements_Comparison of loading speed of html statements on different devices [Introduction] Feb 07, 2026 am 09:39 AM

HTML loading speed is not determined by tag type, but depends on resource volume, network transmission, browser parsing efficiency and device performance differences; mobile terminals need to avoid blocking scripts, inline styles, deep nesting and uncompressed HTML.

Related articles