MyException - 我的异常网
当前位置:我的异常网» XML/SOAP » Android-用SAX解析xml资料

Android-用SAX解析xml资料

www.MyException.Cn  网友分享于:2013-02-25  浏览:4次
Android--用SAX解析xml文件

主运行类

package com.mrzhu.test31;

import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class SAXParse extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    //按钮事件
    public void sax(View v){
    	startSax();
    }

	private void startSax() {
		//获得解析器工厂
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		try {
			//获得解析器
			SAXParser saxParser = saxParserFactory.newSAXParser();
			//装载要解析的xml文件
			InputStream inputStream = getAssets().open("person.xml");
			SAXHandler saxHandler = new SAXHandler();
			//调用parse方法开始解析
			saxParser.parse(inputStream, saxHandler);
			//取得解析后的结果
			List<Person> list = saxHandler.getList();
			for (Person person : list) {
				Log.i("System.out", person.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


 

 

 

SAXHandler类:

package com.mrzhu.test31;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXHandler extends DefaultHandler {
	private List<Person> list;
	private Person currPerson;
	private String tag;
	
	public List<Person> getList() {
		return list;
	}

	/**
	 * 解析到开始标记时执行该方法
	 */
	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		//实例化一个list用于存储解析出来的对象
		list = new ArrayList<Person>();
	}

	/**
	 * 解析到开始元素时执行该方法
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		//当开始标签为person时实例化一个Person对象
		if("person".trim().equalsIgnoreCase(qName)){
			currPerson = new Person();
			//取得属性值
			currPerson.setPersonid(attributes.getValue(qName));
		}
		//将解析到的标签赋值给tag
		tag = qName;
	}


	/**
	 * 当解析到内容时执行该方法
	 */
	@Override
	public void characters(char[] ch, int start, int length)
	throws SAXException {
		super.characters(ch, start, length);
		if(tag != null){
			//判断解析出来的标签名,则取出对应值并设置给Person对象 
			if("name".trim().equalsIgnoreCase(tag)){
				currPerson.setName(new String(ch, start, length));
			}else if("address".trim().equalsIgnoreCase(tag)){
				currPerson.setAddress(new String(ch, start, length));
			}else if("tel".trim().equalsIgnoreCase(tag)){
				currPerson.setTel(new String(ch, start, length));
			}else if("fax".trim().equalsIgnoreCase(tag)){
				currPerson.setFax(new String(ch, start, length));
			}else if("email".trim().equalsIgnoreCase(tag)){
				currPerson.setEmail(new String(ch, start, length));
			}
		}
	}

	/**
	 * 当解析到结束标签时执行该方法
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
	throws SAXException {
		super.endElement(uri, localName, qName);
		//若标签名为person,则将Person对象加到list集合中
		if("person".trim().equalsIgnoreCase(qName)){
			list.add(currPerson);
		}
		tag = null;
	}

	/**
	 * 当解析到结束文档标签时执行该方法
	 */
	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
		
	}
}


 

 

Person类:

 

package com.mrzhu.test31;

public class Person {
	private String personid;
	private String name;
	private String address;
	private String tel;
	private String fax;
	private String email;
	public String getPersonid() {
		return personid;
	}
	public void setPersonid(String personid) {
		this.personid = personid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getFax() {
		return fax;
	}
	public void setFax(String fax) {
		this.fax = fax;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", address=" + address + ", tel=" + tel
				+ ", fax=" + fax + ", email=" + email + "]";
	}
	
}


 

 

person.xml文件:

 

<?xml version="1.0" encoding="UTF-8"?>
<people>
	<person personid="E01">
		<name>Tony Blair</name>
		<address>10 Downing Street, London, UK</address>
		<tel>(061) 98765</tel>
		<fax>(061) 98765</fax>
		<email>blair@everywhere.com</email>
	</person>
	<person personid="E02">
		<name>Bill Clinton</name>
		<address>White House, USA</address>
		<tel>(001) 6400 98765</tel>
		<fax>(001) 6400 98765</fax>
		<email>bill@everywhere.com</email>
	</person>
	<person personid="E03">
		<name>Tom Cruise</name>
		<address>57 Jumbo Street, New York, USA</address>
		<tel>(001) 4500 67859</tel>
		<fax>(001) 4500 67859</fax>
		<email>cruise@everywhere.com</email>
	</person>
	<person personid="E04">
		<name>Linda Goodman</name>
		<address>78 Crax Lane, London, UK</address>
		<tel>(061) 54 56789</tel>
		<fax>(061) 54 56789</fax>
		<email>linda@everywhere.com</email>
	</person>
</people>


 

 

main.xml文件:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="sax"
        android:text="@string/btn_sax" />

</LinearLayout>


 

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有