XML文件解析并利用SimpleAdapter将解析结果显示在Activity中
首先创建一个实体类 Mp3Info用来存储解析的XML文件中的内容:
public class Mp3Info implements Serializable{
private static final long serialVersionUID = 1L;
private String id;
private String mp3Name;
private String mp3Size;
private String lrcName;
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 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 getLrcSize() {
return lrcSize;
}
public void setLrcSize(String lrcSize) {
this.lrcSize = lrcSize;
}
public Mp3Info(String id, String mp3Name, String mp3Size, String lrcName,
String lrcSize) {
super();
this.id = id;
this.mp3Name = mp3Name;
this.mp3Size = mp3Size;
this.lrcName = lrcName;
this.lrcSize = lrcSize;
}
public Mp3Info() {
super();
}
@Override
public String toString() {
return "Mp3Info [id=" + id + ", mp3Name=" + mp3Name + ", mp3Size="
+ mp3Size + ", lrcName=" + lrcName + ", lrcSize=" + lrcSize
+ "]";
}
}
文件下载后将文件存入一个String类型的变量中,从而对文件进行解析,需创建一个Mp3ListContentHandler类,用来定义对读取的String字符串的解析过程:
private List<Mp3Info> parse(String xmlStr){
//创建一个SaxParserFactory
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
//创建List数组,其中数据类型为Mp3Info
List<Mp3Info> infos = new ArrayList<Mp3Info>();
try{
//生成XMLReader对象
XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(infos);
//为XMLReader设置内容处理器,当遇到文件标签响应的位置时,触发相应的事件
xmlReader.setContentHandler(mp3ListContentHandler);
//开始解析文件,StringReader是IO流中的一个类,专门以流的方式处理一个字符串
xmlReader.parse(new InputSource(new StringReader(xmlStr)));
for(Iterator iterator = infos.iterator();iterator.hasNext();){
Mp3Info mp3Info = (Mp3Info) iterator.next(); //此方法可以得到infos中的每一个Mp3Info对象
}
}catch(Exception e){
e.printStackTrace();
}
return infos; //此时得到的Infos对象,为XMLStr中的所有的元素Mp3Name,Mp3Size,LrcName,LrcSize
}
Mp3ListContentHandler类的定义如下:
public class Mp3ListContentHandler extends DefaultHandler{
private List<Mp3Info> infos = null;
private Mp3Info mp3Info = null;
//构造函数,在对象创建时,将infos对象进行赋值
public Mp3ListContentHandler(List<Mp3Info> infos) {
super();
this.infos = infos;
}
private String tagName = null;
public List<Mp3Info> getInfos() {
return infos;
}
public void setInfos(List<Mp3Info> infos) {
this.infos = infos;
}
//当开始解析文件时,就会调用startDocument方法
@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 {
//uri就是当前正在解析的XML文件的命名空间,localName不带前缀的标签名称,qName是带前缀的标签名,attributes代表的是标签的属性
this.tagName = localName;
if(tagName.equals("resource")){
//当遇到resource时生成一个mp3Info对象
mp3Info = new Mp3Info();
}
}
/**
* 得到标签当中的内容,将当前内容存入ch的start开始,长度为length的数组中
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String temp = new String(ch,start,length);
if(tagName.equals("id")){
mp3Info.setId(temp);
}
else if(tagName.equals("mp3.name")){
mp3Info.setMp3Name(temp);
}
else if(tagName.equals("mp3.size")){
mp3Info.setMp3Size(temp);
}
else if(tagName.equals("lrc.name")){
mp3Info.setLrcName(temp);
}
else if(tagName.equals("lrc.size")){
mp3Info.setLrcSize(temp);
}
super.characters(ch, start, length);
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(qName.equals("resource")){
//当遇到标签结尾时,将当前对象加入到List<Mp3Info>当中
infos.add(mp3Info);
}
tagName = "";
}
}
将解析得到的infos结果显示在Activity当中:
private void UpdataListView(){
//下载包含所有Mp3基本信息的xml文件
String xml = downloadXML("http://192.168.1.105:8080/mp3/resources.xml");
//对XML文件进行解析,并将解析的结果放入Mp3Info对象当中,最后将这些Mp3Info对象放置到List当中
infos = parse(xml);
//将SimpleAdapter对象设置到ListActivity当中
SimpleAdapter simpleAdapter = buildSimpleAdapter(infos);
Mp3ListActivity.this.setListAdapter(simpleAdapter);
}
private SimpleAdapter buildSimpleAdapter(List<Mp3Info> infos){
//生成一个List对象,并按照SimpleAdapter的标准,将infos当中的数据添加到List当中去
List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
for(Iterator iterator = infos.iterator();iterator.hasNext();){
Mp3Info mp3Info = (Mp3Info) iterator.next();
HashMap<String,String> map = new HashMap<String,String>();
map.put("mp3_name", mp3Info.getMp3Name());
map.put("size_id", mp3Info.getMp3Size());
list.add(map);
}
//创建一个SimpleAdapter对象
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
R.layout.mp3info_item, new String[] { "mp3_name", "size_id" },
new int[] { R.id.mp3_name, R.id.size_id });
// 将这个SimpleAdapter对象设置到ListActivity当中
return simpleAdapter;
}