Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 667|回复: 0

[实例教程]Android开发之 SAX解析xml实例教程

[复制链接]

该用户从未签到

发表于 2011-10-22 12:28:26 | 显示全部楼层 |阅读模式
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;

}

}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-12-23 09:43 , Processed in 1.762699 second(s), 36 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表