Android开发(7)--Android中的SAX解析XML文件
一、在assets文件中写一个xml文件
[html] v
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="23">
<name>李明</name>
<age>30</age>
</person>
<person id="20">
<name>李向梅</name>
<age>25</age>
</person>
</persons>
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="23">
<name>李明</name>
<age>30</age>
</person>
<person id="20">
<name>李向梅</name>
<age>25</age>
</person>
</persons>
二、在service中写一个SAX解析的类
[java]
package com.example.service;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.example.domain.Person;
public class XMLContentHandlerextends DefaultHandler{
//解析的person对象存储到list集合中
private List<Person> persons;
//解析当前的person对象
public Person currentPerson;
//声明标签的名称
public String tagName;
//获取解析所有的person对象
public List<Person> getPersons() {
return persons;
}
@Override
public void characters(char[] ch,int start, int length)
throws SAXException {
super.characters(ch, start, length);
//首先判断tagName是否为空
if(tagName!=null){
String data=new String(ch,start,length);
//判断标签是否为空
if(tagName.equals("name")){
currentPerson.setName(data);
}else if(tagName.equals("age")){//判断标签是否是age
currentPerson.setAge((short) Integer.parseInt(data));
}
}
}
@Override
public void endDocument()throws SAXException {
super.endDocument();
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
//当person标签结束的时候把person对象存储到集合中
if(localName.equals("person")){
persons.add(currentPerson);
currentPerson=null;
}
this.tagName=null;
}
/**
* 文档开始触发的事件
*/
@Override
public void startDocument()throws SAXException {
super.startDocument();
persons=new ArrayList<Person>();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
//判断解析的标签是否是person
if(localName.equals("person")){
currentPerson=new Person();
//把id属性的值解析出来 并且把id赋值给currentPerson对象
currentPerson.setId(Integer.parseInt(attributes.getValue(0)));
}
this.tagName=localName;
}
}
[java]
package com.example.service;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.example.domain.Person;
public class XMLContentHandler extends DefaultHandler{
//解析的person对象存储到list集合中
private List<Person> persons;
//解析当前的person对象
public Person currentPerson;
//声明标签的名称
public String tagName;
//获取解析所有的person对象
public List<Person> getPersons() {
return persons;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
//首先判断tagName是否为空
if(tagName!=null){
String data=new String(ch,start,length);
//判断标签是否为空
if(tagName.equals("name")){
currentPerson.setName(data);
}else if(tagName.equals("age")){//判断标签是否是age
currentPerson.setAge((short) Integer.parseInt(data));
}
}
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
//当person标签结束的时候把person对象存储到集合中
if(localName.equals("person")){
persons.add(currentPerson);
currentPerson=null;
}
补充:移动开发 , Android ,