Android SAX解析实例教程
SAX(simpleAPIforXML)是一种XML解析的替代方法。相比于DOM,SAX是一种速度更快,更有效的方法。它逐行扫描文档,一边扫描一边解析。而且相比于DOM,SAX可以在解析文档的任意时刻停止解析,但任何事物都有其相反的一面,对于SAX来说就是操作复杂。
下面对resources.xml文件进行解析
1、源文件如下
<resources>
<resource>
<mp3.name>kong</mp3.name>
<mp3.author>侧田</mp3.author>
</resource>
<resource>
<mp3.name>蚂蚁</mp3.name>
<mp3.author>古巨基</mp3.author>
</resource>
</resources>
2、创建Mp3Info.java实体bean类
package cn.m9;
public class Mp3Info {
private String mp3Name;
private String author;
public Mp3Info() {
}
public Mp3Info(String mp3Name, String author) {
super();
this.mp3Name = mp3Name;
this.author = author;
}
public String getMp3Name() {
return mp3Name;
}
public void setMp3Name(String mp3Name) {
this.mp3Name = mp3Name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Mp3Info [author=" + author + ", mp3Name=" + mp3Name + "]";
}
}
3、创建Mp3ListContentHandler.java类,继承DefaultHandler类
package cn.m9;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Mp3ListContentHandler extends DefaultHandler {
private Mp3Info mp3Info = null;
private List<Mp3Info> mp3Infos = null;
private String tagName = null;
public Mp3ListContentHandler() {
super();
}
public Mp3ListContentHandler(List<Mp3Info> mp3Infos) {
super();
this.mp3Infos = mp3Infos;
}
public List<Mp3Info> getMp3Infos() {
return mp3Infos;
}
public void setMp3Infos(List<Mp3Info> mp3Infos) {
this.mp3Infos = mp3Infos;
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
tagName = localName;
if (tagName.equals("resource")) {
mp3Info = new Mp3Info();
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String tmp = new String(ch, start, length);
if (tagName.equals("mp3.name")) {
mp3Info.setMp3Name(tmp);
} else if (tagName.equals("mp3.author")) {
mp3Info.setAuthor(tmp);
}
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
tagName = localName;
if (tagName.equals("resource")) {
mp3Infos.add(mp3Info);
}
tagName = "";
}
}
4、创建TestSAXActivity.java类
package cn.m9;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TestSAXActivity extends Activity {
/** Called when the activity is first created. */
private String xml = "<resources><resource><mp3.name>kong</mp3.name><mp3.author>侧田</mp3.author></resource><resource><mp3.name>蚂蚁</mp3.name><mp3.author>古巨基</mp3.author></resource></resources>";
private LinearLayout layout = null;
private TextView tv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (LinearLayout) this.findViewById(R.id.main);
List<Mp3Info> infos = parseXml(xml);
String text = null;
for (Iterator i = infos.iterator(); i.hasNext();) {
Mp3Info mp3 = (Mp3Info) i.next();
text = "" + mp3.getMp3Name() + " 演唱:"
+ mp3.getAuthor();
tv = new TextView(this);
tv.setText(text);
tv.setWidth(500);
tv.setTextSize(20);
layout.addView(tv);
}
}
public List<Mp3Info> parseXml(String xmlStr) {
// 创建SAXParserFactory解析器工厂
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
List<Mp3Info> mp3Infos = null;
Mp3Info mp3Info = null;
try {
// 创建XMLReader对象,xml文件解析器
XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
mp3Infos = new ArrayList<Mp3Info>();
// 注册内容事件处理器(设置xml文件解析器的解析方式)
xmlReader.setContentHandler(new Mp3ListContentHandler(mp3Infos));
// 开始解析xml格式文件
xmlReader.parse(new InputSource(new StringReader(xmlStr)));
for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
mp3Info = (Mp3Info) mp3Infos.iterator();
}
mp3Infos.add(mp3Info);
} catch (Exception e) {
e.printStackTrace();
}
return mp3Infos;
}
}
效果图:
下面对resources.xml文件进行解析
1、源文件如下
<resources>
<resource>
<mp3.name>kong</mp3.name>
<mp3.author>侧田</mp3.author>
</resource>
<resource>
<mp3.name>蚂蚁</mp3.name>
<mp3.author>古巨基</mp3.author>
</resource>
</resources>
2、创建Mp3Info.java实体bean类
package cn.m9;
public class Mp3Info {
private String mp3Name;
private String author;
public Mp3Info() {
}
public Mp3Info(String mp3Name, String author) {
super();
this.mp3Name = mp3Name;
this.author = author;
}
public String getMp3Name() {
return mp3Name;
}
public void setMp3Name(String mp3Name) {
this.mp3Name = mp3Name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Mp3Info [author=" + author + ", mp3Name=" + mp3Name + "]";
}
}
3、创建Mp3ListContentHandler.java类,继承DefaultHandler类
package cn.m9;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Mp3ListContentHandler extends DefaultHandler {
private Mp3Info mp3Info = null;
private List<Mp3Info> mp3Infos = null;
private String tagName = null;
public Mp3ListContentHandler() {
super();
}
public Mp3ListContentHandler(List<Mp3Info> mp3Infos) {
super();
this.mp3Infos = mp3Infos;
}
public List<Mp3Info> getMp3Infos() {
return mp3Infos;
}
public void setMp3Infos(List<Mp3Info> mp3Infos) {
this.mp3Infos = mp3Infos;
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
tagName = localName;
if (tagName.equals("resource")) {
mp3Info = new Mp3Info();
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String tmp = new String(ch, start, length);
if (tagName.equals("mp3.name")) {
mp3Info.setMp3Name(tmp);
} else if (tagName.equals("mp3.author")) {
mp3Info.setAuthor(tmp);
}
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
tagName = localName;
if (tagName.equals("resource")) {
mp3Infos.add(mp3Info);
}
tagName = "";
}
}
4、创建TestSAXActivity.java类
package cn.m9;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TestSAXActivity extends Activity {
/** Called when the activity is first created. */
private String xml = "<resources><resource><mp3.name>kong</mp3.name><mp3.author>侧田</mp3.author></resource><resource><mp3.name>蚂蚁</mp3.name><mp3.author>古巨基</mp3.author></resource></resources>";
private LinearLayout layout = null;
private TextView tv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (LinearLayout) this.findViewById(R.id.main);
List<Mp3Info> infos = parseXml(xml);
String text = null;
for (Iterator i = infos.iterator(); i.hasNext();) {
Mp3Info mp3 = (Mp3Info) i.next();
text = "" + mp3.getMp3Name() + " 演唱:"
+ mp3.getAuthor();
tv = new TextView(this);
tv.setText(text);
tv.setWidth(500);
tv.setTextSize(20);
layout.addView(tv);
}
}
public List<Mp3Info> parseXml(String xmlStr) {
// 创建SAXParserFactory解析器工厂
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
List<Mp3Info> mp3Infos = null;
Mp3Info mp3Info = null;
try {
// 创建XMLReader对象,xml文件解析器
XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
mp3Infos = new ArrayList<Mp3Info>();
// 注册内容事件处理器(设置xml文件解析器的解析方式)
xmlReader.setContentHandler(new Mp3ListContentHandler(mp3Infos));
// 开始解析xml格式文件
xmlReader.parse(new InputSource(new StringReader(xmlStr)));
for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
mp3Info = (Mp3Info) mp3Infos.iterator();
}
mp3Infos.add(mp3Info);
} catch (Exception e) {
e.printStackTrace();
}
return mp3Infos;
}
}
效果图: