安卓sax解析xml

安卓sax解析xml_第1张图片新建raw文件夹下的xml文件做为解析测试的xml

dashu.xml



	
		大树
		22
	
	
	
		梅子
		28
	

	
		爸爸
		48
	
	
	
		妈妈
		48
	


 

activity_main.xml



    
    


 

items.xml是listview的item




    


    

    


MyHandler继承DefaultHandler解析工具类

package com.example.dashu_saxxml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.R.integer;

public class MyHandler extends DefaultHandler {
	private HashMap map = null;// 存储单个解析的完整对象
	List> list = null;// 存储所以的解析对象
	private String currentTag = null;// 正在解析元素的标签
	private String currentValue = null;// 正在解析元素的值
	private String nodeName = null;// 当前解析节点名称

	public MyHandler(String nodeName) {
		super();
		this.nodeName = nodeName;
	}

	public List> getList() {
		return list;
	}

	/**
	 * xml文件开始解析时候调用的方法
	 * */
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		list = new ArrayList>();
		super.startDocument();
	}

	/**
	 * 解析到节点开头调用方法
	 * */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		if (qName.equals(nodeName)) {
			map = new HashMap();
		}
		if (attributes != null && map != null) {
			for (int i = 0; i < attributes.getLength(); i++) {
				map.put(attributes.getQName(i), attributes.getValue(i));
			}
		}
		currentTag = qName;
	}

	/**
	 * 解析到节点开头结尾中间夹的文字所调用的方法
	 * */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		if (currentTag != null && map != null) {
			currentValue = new String(ch, start, length);
			if (currentValue != null && !currentValue.trim().equals("")
					&& !currentValue.trim().equals("\n")) {
				map.put(currentTag, currentValue);
			}
		}
		currentTag = null;// 把当前节点对应的值和标签设置为空
		currentValue = null;
	}

	/**
	 * 解析到节点结尾调用方法
	 * */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// 遇到结束标记时候
		if (qName.equals(nodeName)) {
			list.add(map);
			map = null;
		}
	}
}


SaxService

package com.example.dashu_saxxml;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;

public class SaxService {
	public SaxService() {
	}

	/**
	 * 网络xml解析方式
	 * */
	public static List> readXML(
			InputStream inputStream, String nodeName) {
		List>list=null;
		try {
			//创建一个解析xml工厂对象
			SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();
			SAXParser parser=saxParserFactory.newSAXParser();//解析xml
			MyHandler myHandler=new MyHandler(nodeName);
			parser.parse(inputStream, myHandler);
			list=myHandler.getList();
			inputStream.close();//关闭io
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}
}


MainActivity

package com.example.dashu_saxxml;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.os.Build;

public class MainActivity extends Activity {
	List> list = null;
	InputStream inputStream = null;
	SimpleAdapter simpleAdapter;
	ListView listView;

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

		inputStream = getResources().openRawResource(R.raw.dashu);
		list = SaxService.readXML(inputStream, "user");
		listView = (ListView) this.findViewById(R.id.listview);
		simpleAdapter = new SimpleAdapter(getApplicationContext(), list,
				R.layout.litems, new String[] { "id", "name", "age" },
				new int[] { R.id.textView1, R.id.textView2, R.id.textView3 });
		listView.setAdapter(simpleAdapter);
	}

}


安卓sax解析xml_第2张图片

你可能感兴趣的:(安卓数据解析)