WEB开发网
开发学院手机开发Android 开发 Android中Sax解析与Dom解析xml文件 阅读

Android中Sax解析与Dom解析xml文件

 2013-05-30 20:52:59 来源:WEB开发网   
核心提示: <?xml version="1.0" encoding="UTF-8"?><persons><person id="23"><name>李明</name><age>30</age

 <?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>
<person id="24">
<name>常瑞鹏</name>
<age>65</age>
</person>
</persons>
------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------
package com.example.day03_xml.domain;



import java.io.Serializable;


public class Person implements Serializable{

/**

*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private short age;

public Person() {
super();
// TODO Auto-generated constructor stub
}


public Person(int id, String name, short age) {
super();
this.id = id;
this.name = name;
this.age = age;
}


public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public short getAge() {
return age;
}


public void setAge(short age) {
this.age = age;
}


 

}
-------------------------------------------------------------------------------------------------------------------------------------------------
package com.example.day03_xml.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.day03_xml.domain.Person;


public class XMLContentHandlerService extends DefaultHandler {
// 解析的person对象储存到list集合中
private List<Person> persons;
// 解析当前person对象
public Person currenPerson;
// 声明标签名称
public String tagName;


public List<Person> getPersons() {
return persons;
}


public void setPersons(List<Person> persons) {
this.persons = 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);
// 判断标签是否是name
if (tagName.equals("name")) {
currenPerson.setName(data);
} else if (tagName.equals("age")) {// 判断标签是否是age
currenPerson.setAge((short) Integer.parseInt(data));
}


}
}


@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(currenPerson);
// 当对象=null
currenPerson = null;
}


// person age
// name
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")) {
// 创建person对象
currenPerson = new Person();
// 把id属性的值解析出来 并且把id赋值给currentPerson对象
currenPerson.setId(Integer.parseInt(attributes.getValue(0)));
}
// person name age
this.tagName = localName;


}


public void endDocument() throws SAXException {
super.endDocument();
}


}

1 2 3 4  下一页

Tags:Android Sax 解析

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接