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)

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.

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.

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

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