android学习之xml解析

我们使用的很多文档都是以xml为基础的标记语言,今天我就写一篇基于android的xml解析,在安卓中主要有三种解析的方式,一个是android添加的pull解析,还有两种就是java的dom和sax解析,dom解析式一次性将所有的内容读到内存当中,而pull和sax是边读边解析,在需要重复读取的小文件是,我们更多的是需要dom解析这种格式。个人更加喜欢pullsax解析,因为感觉逼格更高,代码如下,

package com.jk.xmlparser;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void onMyClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btn_dom:
			domParser();
			break;
		case R.id.btn_pull:
			pullParser();
			break;
		case R.id.btn_sax:
			saxParser();
			break;
		}

	}

	private void saxParser() {
		try {
			// get factory
			SAXParserFactory factory = SAXParserFactory.newInstance();
			// get the parser from the factory
			SAXParser parser = factory.newSAXParser();
			// get the xml file
			InputStream is = getAssets().open("activity_main.xml");
			// use the callback to parse the file
			parser.parse(is, new MyHandler());
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	class MyHandler extends DefaultHandler {
		private boolean canReadText = false;

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			System.out.println("character" + new String(ch, start, length));
		}

		@Override
		public void endDocument() throws SAXException {
			// TODO Auto-generated method stub
			System.out.println("document parsing out");

		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if ("Button".equals(localName))
				canReadText = false;

		}

		@Override
		public void startDocument() throws SAXException {
			// the begin of the Document
			System.out.println("document parsing begins");
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			// the begin of the node
			if ("Button".equals(localName))
				canReadText = true;

		}

	}

	private void pullParser() {
		try {
			// // 获取工厂
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			// //获取解析器
			XmlPullParser parser = factory.newPullParser();
			// //获取需要解析的文件
			InputStream is = getAssets().open("activity_main.xml");
			// //将解析的文件给解析器
			parser.setInput(is, "UTF8");
			while (true) {
				// //移动游标向后读取数据,并获得当前的节点的类型
				int type = parser.next();
				// getTagName
				String tagName = parser.getName();
				switch (type) {
				case XmlPullParser.START_DOCUMENT:
					System.out.println("StART_DOCUMENT");
					break;
				case XmlPullParser.END_DOCUMENT:
					// 读到后面就返回
					return;

				case XmlPullParser.START_TAG:
					// the begin of a node
					System.out.println(tagName + "begin_parser");
					break;
				case XmlPullParser.END_TAG:
					// the end of node
					System.out.println(tagName + "end_parser");
					break;
				case XmlPullParser.TEXT:
					System.out.println("Text" + parser.getText());
					break;

				}
			}

		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void domParser() {
		// 创建DocumentBuilderFactory的工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			// 通过factor获得DocumentBuilder的对象
			DocumentBuilder builder = factory.newDocumentBuilder();
			// 获的保存在assets中的xml文件
			InputStream is = getAssets().open("activity_main.xml");
			// 解析指定的XML文件,这里我们需要导入的包是w3c里面的
			Document document = builder.parse(is);
			// 上面的四步完成以后,xml文件就已经被解析到了内存中,形成了xml标签树
			NodeList list = document.getElementsByTagName("Button");
			for (int i = 0; i < list.getLength(); i++) {
				// 因为我们大多数时候需要取值,所以我就没有写遍历属性
				// 这里需要注意在两个tag之间,必须有内容,否则就会出现空指针的错误
				System.out.println("标签的值: "
						+ list.item(i).getFirstChild().getNodeValue());
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


你可能感兴趣的:(Android基础学习)