注意:
当读到一个开始标签的时候,会触发这个方法。uri是命名空间(通过xmlns声明),localName是不带命名空间前缀的标签名,qName是带命名空间前缀的标签名。通过atts可以得到所有的属性名和相应的值。注意,如果没有指定Namespace,则qName可能为空,当然不同的SAX实现会有所不同,比如在Android中qName为空,而J2SE中localName为空,所以想要总是得到标签名,就需要检查这两个参数的值了。
下面开始SAX解析xml
要解析的xml文件如下 mp3list.xml
<?xml version="1.0" encoding="utf-8"?>
<resource>
<mp3>
<id>1</id>
<mp3Name>baby</mp3Name>
<mp3Url>mp3</mp3Url>
<mp3Size>3426887</mp3Size>
<lrcName>baby</lrcName>
<lrcUrl>lrc</lrcUrl>
<lrcSize>1699</lrcSize>
</mp3>
<mp3>
<id>2</id>
<mp3Name>兰亭序</mp3Name>
<mp3Url>mp3</mp3Url>
<mp3Size>4066289</mp3Size>
<lrcName>兰亭序</lrcName>
<lrcUrl>lrc</lrcUrl>
<lrcSize>1812</lrcSize>
</mp3>
</resource>
用SAX解析xml文件主要是实现DefaultHandler接口
package dada.mp3player.xml;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
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 dada.mp3player.model.Mp3Bean;
/**
*
* <p>Title: Android project </p>
* <p>Description: 用SAX解析mp3list.xml文件</p>
* <p>Copyright: Copyright (c) 2011 - 2012</p>
* <p>Company: dada </p>
* @author <a href="mailTo:dada360778511@sina.com">dada</a>
* @version 1.0
* @history:
*/
public class Mp3ContentHandler extends DefaultHandler{
private String currentTagName;
private List mp3List =null;
private Mp3Bean mp3 = null;
private final String MP3_TAG = "mp3";
@Override
public void startDocument() throws SAXException {
System.out.println("startDocument");
mp3List = new ArrayList();
}
@Override
public void endDocument() throws SAXException {
// mp3List = null;
// mp3 = null;
System.out.println("endDocument");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
this.currentTagName = localName;
System.out.println("StartEle:localName="+localName+" qName="+qName+" uri="+uri);
if(this.currentTagName.equals(MP3_TAG)){
this.mp3 = new Mp3Bean();
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("endEle:localName="+localName+" qName="+qName+" uri="+uri);
if(qName.equals(MP3_TAG)){
mp3List.add(this.mp3);
this.mp3 = null;
}
//注意这里千万注意
this.currentTagName = "";
}
public List getMp3List() {
return this.mp3List;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String tempString = new String(ch,start,length);
System.out.println("characters tempString:"+tempString+" currentTagName:"+currentTagName);
if("id".equals(this.currentTagName)){
this.mp3.setId(tempString);
}else if("mp3Name".equals(currentTagName)){
this.mp3.setMp3Name(tempString);
}else if("mp3Url".equals(currentTagName)){
this.mp3.setMp3Url(tempString);
}else if("mp3Size".equals(currentTagName)){
this.mp3.setMp3Size(tempString);
}else if("lrcName".equals(currentTagName)){
this.mp3.setLrcName(tempString);
}else if("lrcUrl".equals(currentTagName)){
this.mp3.setLrcUrl(tempString);
}else if("lrcSize".equals(currentTagName)){
this.mp3.setLrcSize(tempString);
}
}
public static void main(String[] args) {
Mp3ContentHandler handler = new Mp3ContentHandler();
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try {
XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
File xmlFile = new File("E:\\android_dev\\apache-tomcat-6.0.14\\webapps\\mp3player\\resource\\mp3list.xml");
FileInputStream is = new FileInputStream(xmlFile);
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(is));
List mp3List = handler.getMp3List();
for(int i=0;i<handler.getMp3List().size();i++){
System.out.println(mp3List.get(i));
}
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
mp3Bean.java
package dada.mp3player.model;
public class Mp3Bean {
private String id;
private String mp3Name;
private String mp3Url;
private String mp3Size;
private String lrcName;
private String lrcUrl;
private String lrcSize;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMp3Name() {
return mp3Name;
}
public void setMp3Name(String mp3Name) {
this.mp3Name = mp3Name;
}
public String getMp3Url() {
return mp3Url;
}
public void setMp3Url(String mp3Url) {
this.mp3Url = mp3Url;
}
public String getMp3Size() {
return mp3Size;
}
public void setMp3Size(String mp3Size) {
this.mp3Size = mp3Size;
}
public String getLrcName() {
return lrcName;
}
public void setLrcName(String lrcName) {
this.lrcName = lrcName;
}
public String getLrcUrl() {
return lrcUrl;
}
public void setLrcUrl(String lrcUrl) {
this.lrcUrl = lrcUrl;
}
public String getLrcSize() {
return lrcSize;
}
public void setLrcSize(String lrcSize) {
this.lrcSize = lrcSize;
}
@Override
public String toString() {
return "Map3Bean [id=" + id + ", mp3Name=" + mp3Name + ", mp3Url="
+ mp3Url + ", mp3Size=" + mp3Size + ", lrcName=" + lrcName
+ ", lrcUrl=" + lrcUrl + ", lrcSize=" + lrcSize + "]";
}
}