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>
<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();
}
}
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();
}
}
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接