SAX解析XML文件

抛砖引玉,是高手,看代码就行,一看就懂,为了节约时间,注释和解说就不写了,累赘

package com.van.SAXparse;

public class Person
{
private int id;

private String name;

private Short age;

private int height;

private String profession;

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

public String getName()
{
return name;
}

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

public Short getAge()
{
return age;
}

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

public int getHeight()
{
return height;
}

public void setHeight(int height)
{
this.height = height;
}

public String getProfession()
{
return profession;
}

public void setProfession(String profession)
{
this.profession = profession;
}
}

 

package com.van.SAXparse;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

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

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

import android.content.Context;

public class XMLContentHandler extends DefaultHandler
{
private static List<Person> persons = null;

private Person currentPerson;

static Context context;

private String tagName = null;// 当前解析的元素标签

public List<Person> getPersons()
{
return persons;
}

/**
* 接收文档的开始通知
*
*
@throws SAXException
*/
@Override
public void startDocument() throws SAXException
{
persons = new ArrayList<Person>();
}

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

/**
* 接收元素开始的通知
*
*
@param uri 元素的命名空间
*
@param localName 元素的本地名称(不带前缀)
*
@param qName 元素的限定名(带前缀)
*
@param attributes 元素的属性集合
*
@throws SAXException
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
if (localName.equals("person"))
{
currentPerson = new Person();
currentPerson.setId(Integer.parseInt(attributes.getValue("id")));
}
this.tagName = localName;
}

/**
* 接收文档的结尾的通知
*
*
@param uri 元素的命名空间
*
@param localName 元素的本地名称(不带前缀)
*
@param qName 元素的限定名(带前缀)
*
@throws SAXException
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
if (localName.equals("person"))
{
persons.add(currentPerson);
currentPerson = null;
}
this.tagName = null;
}

/**
* 接收字符数据的通知
*
*
@param ch
*
@param start
*
@param length
*
@throws SAXException
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
if (tagName != null)
{
String data = new String(ch, start, length);
if (tagName.equals("name"))
{
this.currentPerson.setName(data);
}
else if (tagName.equals("age"))
{
this.currentPerson.setAge(Short.parseShort(data));
}
else if (tagName.equals("height"))
{
this.currentPerson.setHeight(Integer.parseInt(data));
}
else if (tagName.equals("profession"))
{
this.currentPerson.setProfession(data);
}
}
}

public static List<Person> readXML(String xmlPath)
{
try
{
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
// 创建解析器
SAXParser saxParser = saxParserFactory.newSAXParser();
// 获取事件源
XMLReader xmlReader = saxParser.getXMLReader();
// 设置处理器
XMLContentHandler xmlContentHandler = new XMLContentHandler();
xmlReader.setContentHandler(xmlContentHandler);
// 解析xml文档
xmlReader.parse(new InputSource(new URL(xmlPath).openStream()));
// xmlReader.parse(new
// InputSource(context.getAssets().open(xmlPath)));
persons = xmlContentHandler.getPersons();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return persons;
}

}

 

package com.van.SAXparse;

import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SAXparseActivity extends Activity
{
private Button button;

private Person person;

private TextView textView;

private List<Person> persons;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.showPerson);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
persons = XMLContentHandler.readXML("D:/person.xml");
for (int i = 0; i < persons.size(); i++)
{
person = persons.get(i);
person.getAge();
person.getName();
person.getHeight();
person.getProfession();
person.getId();
String string = "";
textView.setText("ID:" + person.getId());
}
}
});
}
}





posted @ 2012-01-03 16:33  程序学习笔记  阅读(307)  评论(0编辑  收藏  举报