为了去面试今天开始就对android的一些基础知识进行复习。下面对今天学习内容作个总结:采用SAX解析xml文档
首先要明白sax是采用事件驱动来解析整个文档,当触发某个事件的时候回调相应的函数。我们所要做的工作就是在这些回调函数中去实现,那么怎样才能回调那些事件处理函数呢?
在这里其实很简单实现ContentHandler接口接口重写我们需要处理事件的函数就OK!But Android为我们提供了一和帮助类DefaultHandler这个类对那些事件处理函数进行了空实现我们只需去继承这个类实现我们需要处理的函数就行了那么下面我们就来开始实现这个过程吧!
这里的xml文件person.xml
---> <?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="23">
<name>liming</name>
<age>30</age>
</person>
<person id="20">
<name>lixiangmei</name>
<age>25</age>
</person>
</persons>
这里我用SAXPersonService来作逻辑层
public class SAXPersonService {
public List<Person> getPersons(InputStream inStream) throws Throwable{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
PersonParser personParser = new PersonParser();
parser.parse(inStream, personParser);
//这里有许多重载的方法可以使流或文件等
inStream.close();
return personParser.getPersons();
}
public List<Person> getPersons(File file) throws Throwable{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
PersonParser personParser = new PersonParser();
parser.parse(file, personParser);
return personParser.getPersons();
}
private final class PersonParser extends DefaultHandler{
private List<Person> persons = null;
private String tag = null;
private Person person = null;
public List<Person> getPersons() {
return persons;
}
@Override
public void startDocument() throws SAXException {
persons = new ArrayList<Person>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if("person".equals(localName)){
person = new Person();
person.setId(new Integer(attributes.getValue(0)));
}
tag = localName;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(tag!=null){
String data = new String(ch, start, length);//获取文本节点的数据
if("name".equals(tag)){
person.setName(data);
}else if("age".equals(tag)){
person.setAge(new Short(data));
}
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("person".equals(localName)){
persons.add(person);
person = null;
}
tag = null;
}
}
}
下面是我做的单元测试方法
public void readXML()throws Throwable
{
SAXPersonService service = new SAXPersonService();
/*InputStream inStream = getClass().getClassLoader ).getResourceAsStream("person.xml");
*/
//这里是从SD卡中得到文件
File file=new File(Environment.getExternalStorageDirectory(),"person.xml");
//List<Person> persons = service.getPersons(inStream);
List<Person> persons = service.getPersons(file);
for(Person person : persons){
Log.i(TAG, person.toString());
}
}
首先要明白sax是采用事件驱动来解析整个文档,当触发某个事件的时候回调相应的函数。我们所要做的工作就是在这些回调函数中去实现,那么怎样才能回调那些事件处理函数呢?
在这里其实很简单实现ContentHandler接口接口重写我们需要处理事件的函数就OK!But Android为我们提供了一和帮助类DefaultHandler这个类对那些事件处理函数进行了空实现我们只需去继承这个类实现我们需要处理的函数就行了那么下面我们就来开始实现这个过程吧!
这里的xml文件person.xml
---> <?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="23">
<name>liming</name>
<age>30</age>
</person>
<person id="20">
<name>lixiangmei</name>
<age>25</age>
</person>
</persons>
这里我用SAXPersonService来作逻辑层
public class SAXPersonService {
public List<Person> getPersons(InputStream inStream) throws Throwable{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
PersonParser personParser = new PersonParser();
parser.parse(inStream, personParser);
//这里有许多重载的方法可以使流或文件等
inStream.close();
return personParser.getPersons();
}
public List<Person> getPersons(File file) throws Throwable{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
PersonParser personParser = new PersonParser();
parser.parse(file, personParser);
return personParser.getPersons();
}
private final class PersonParser extends DefaultHandler{
private List<Person> persons = null;
private String tag = null;
private Person person = null;
public List<Person> getPersons() {
return persons;
}
@Override
public void startDocument() throws SAXException {
persons = new ArrayList<Person>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if("person".equals(localName)){
person = new Person();
person.setId(new Integer(attributes.getValue(0)));
}
tag = localName;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(tag!=null){
String data = new String(ch, start, length);//获取文本节点的数据
if("name".equals(tag)){
person.setName(data);
}else if("age".equals(tag)){
person.setAge(new Short(data));
}
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("person".equals(localName)){
persons.add(person);
person = null;
}
tag = null;
}
}
}
下面是我做的单元测试方法
public void readXML()throws Throwable
{
SAXPersonService service = new SAXPersonService();
/*InputStream inStream = getClass().getClassLoader ).getResourceAsStream("person.xml");
*/
//这里是从SD卡中得到文件
File file=new File(Environment.getExternalStorageDirectory(),"person.xml");
//List<Person> persons = service.getPersons(inStream);
List<Person> persons = service.getPersons(file);
for(Person person : persons){
Log.i(TAG, person.toString());
}
}