Android中SAX解析XML文件

一、在assets文件中写一个xml文件



	
		李明
		30
	
	
		李向梅
		25
	


二、在service中写一个SAX解析的类

package com.example.service;

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

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

import com.example.domain.Person;

public class XMLContentHandler extends DefaultHandler{

	//解析的person对象存储到list集合中
			private List persons;
			//解析当前的person对象
			public Person currentPerson;
			//声明标签的名称
			public String tagName;
			
			
			//获取解析所有的person对象
			public List getPersons() {
				return persons;
			}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		
		super.characters(ch, start, length);
		//首先判断tagName是否为空
		if(tagName!=null){
			String data=new String(ch,start,length);
			//判断标签是否为空
			if(tagName.equals("name")){
				currentPerson.setName(data);
			}else if(tagName.equals("age")){//判断标签是否是age
				currentPerson.setAge((short) Integer.parseInt(data));
			}
		}
	}

	@Override
	public void endDocument() throws SAXException {
		
		super.endDocument();
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		super.endElement(uri, localName, qName);
		//当person标签结束的时候把person对象存储到集合中 
		if(localName.equals("person")){
			persons.add(currentPerson);
			currentPerson=null;
		}
		
		this.tagName=null;
	}

	/**
	 * 文档开始触发的事件
	 */
	@Override
	public void startDocument() throws SAXException {
		
		super.startDocument();
		persons=new ArrayList();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		
		super.startElement(uri, localName, qName, attributes);
		//判断解析的标签是否是person
		if(localName.equals("person")){
			currentPerson=new Person();
			//把id属性的值解析出来 并且把id赋值给currentPerson对象
			currentPerson.setId(Integer.parseInt(attributes.getValue(0)));
		}
		
		this.tagName=localName;
	}
	
}


三、在Activity中获取SAX类的操作

package com.example.lession03_xml;

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


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

import com.example.domain.Person;
import com.example.service.XMLContentHandler;
import com.example.service.XMLDomService;



import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputBinding;
import android.widget.Button;
import android.widget.Toast;

public class XmlActivity extends Activity {
	
	//声明组件
	public Button btn_sax,btn_dom,btn_pull;

	public XMLDomService xmlDomService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//设置显示的视图
		setContentView(R.layout.activity_xml);
		
		xmlDomService=new XMLDomService();
		//根据id获取组件
		btn_sax=(Button) findViewById(R.id.btn_sax);
		btn_dom=(Button) findViewById(R.id.btn_dom);
		btn_pull=(Button) findViewById(R.id.btn_pull);
		
		//为按钮注册事件
		btn_sax.setOnClickListener(new MyOnclickListener());
		btn_dom.setOnClickListener(new MyOnclickListener());
		btn_pull.setOnClickListener(new MyOnclickListener());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.xml, menu);
		return true;
	}

	//匿名类
	class MyOnclickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			int id=v.getId();
			switch (id) {
			case R.id.btn_sax:
				Toast.makeText(XmlActivity.this, "采用SAX解析", Toast.LENGTH_LONG).show();
				try{
					//SAX解析的工厂对象
					SAXParserFactory factory=SAXParserFactory.newInstance();
					//得到sax的解析器
					SAXParser saxParser=factory.newSAXParser();
					//创建handler对象
					XMLContentHandler handlerService=new XMLContentHandler();
					InputStream is=getAssets().open("csdn.xml");
					//直接解析
					saxParser.parse(is, handlerService);
					//通过handlerService对象获取
					Toast.makeText(XmlActivity.this, "----"+handlerService, Toast.LENGTH_LONG).show();
				}catch(Exception e){
					e.printStackTrace();
				}
				
				break;
			case R.id.btn_dom:
				
				InputStream is=null;
				try{
					//获取读取文件的输入流对象
					is=getAssets().open("csdn.xml");
					//采用dom解析
					List persons=xmlDomService.parseXML(is);
					//简单测试
					//Toast.makeText(XmlActivity.this, ""+persons.get(0).getName(), Toast.LENGTH_LONG).show();
					Toast.makeText(XmlActivity.this, "采用DOM解析"+persons.get(0).getName(), Toast.LENGTH_LONG).show();
				}catch(Exception e){
					e.printStackTrace();
				}
				
				
				break;
			case R.id.btn_pull:
				Toast.makeText(XmlActivity.this, "采用PULL解析", Toast.LENGTH_LONG).show();
				break;

			default:
				break;
			}
			
		}
		
	}
}


 

 

你可能感兴趣的:(Android中SAX解析XML文件)