sax

时间:2023-03-08 23:16:57
sax
<?xml version="1.0" encoding="UTF-8"?>
<beauties>
    <beauty>
        <name>范冰冰</name>
        <age>28</age>
    </beauty>
    <beauty>
        <name>杨幂</name>
        <age>23</age>
    </beauty>
</beauties>

  

<?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" >
    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

  

package com.example.sax;

public class Beauty {

	private String name;
	private String age;

	public void setName(String name) {
		this.name = name;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String toString() {
		return "美女资料 [年龄=" + age + ", 姓名=" + name + "]";
	}
}

  

package com.example.sax;

import java.util.ArrayList;

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

public class MySaxHandler extends DefaultHandler{

	private ArrayList<Beauty> mList;
	private Beauty beauty;
	private String content;

	public MySaxHandler(ArrayList<Beauty> list){
		this.mList = list;
	}

	public void startDocument() throws SAXException {
		super.startDocument();
	}

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

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		content = new String(ch, start, length);
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if("beauty".equals(localName)){
			beauty = new Beauty();
		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		if("name".equals(localName)){
			beauty.setName(content);
		}else if("age".equals(localName)){
			beauty.setAge(content);
		}else if("beauty".equals(localName)){
			mList.add(beauty);
		}
	}
}

  

package com.example.sax;

import java.io.InputStream;
import java.util.ArrayList;

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

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity{

	private ArrayList<Beauty> beautyList;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		if(beautyList == null){
			beautyList = new ArrayList<Beauty>();
		}
		doMyMission();
		setupViews();
	}

	private void doMyMission(){
		try {
			AssetManager as = this.getAssets();
			InputStream is = as.open("beauties.xml");
			InputSource is2 = new InputSource(is);
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			MySaxHandler msh = new MySaxHandler(beautyList);
			xr.setContentHandler(msh);
			xr.parse(is2);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setupViews(){
		String result = "";
		for (Beauty b : beautyList) {
            result += b.toString();
        }
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(result);
	}
}