Android pull封装,直接解析xml
[java]
好久没写过博文了,最近在做xml方面的解析,xml的解析一直都是比较繁琐的,所以对android的 pull进行了简单的封装,不用再为每个xml都创建解析类,但目前只实现了对xml的简单实现,主要通过反射来实现
能解析的xml形式主要有如下几种,复杂的暂未支持
[html]
<?xml version="1.0" encoding="UTF-8"?>
<list>
<user id="1"> www.zzzyk.com
<userName>张三</userName>
<email>zhangsan@xxx.com</email>
</user>
<user>
<id>2</id>
<userName>李四</userName>
<email>lisi@xxx.com</email>
</user>
<user>
<id>3</id>
<userName>王五</userName>
<email>wangwu@xxx.com</email>
</user>
</list>
对应的java类如下:
[java]
public class User {
private int id;
private String userName;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
支持简单的对象解析
[html]
<?xml version="1.0" encoding="UTF-8"?>
<menu>
<id>1</id>
<code>1002</code>
<name>测试</name>
</menu>
或者
[html]
<?xml version="1.0" encoding="UTF-8"?>
<menu id="1" code="1002">
<name>测试</name>
</menu>
对应的java类如下:
[java
public class Menu {
private int id;
private int code;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
支持对象中有list列表的解析
[java]
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<total>33</total>
<list>
<user>
<id>1</id>
<userName>张三</userName>
<email>zhangsan@xxx.com</email>
</user>
<user id="2">
<userName>李四</userName>
<email>lisi@xxx.com</email>
</user>
<user id="3" userName="王五">
<email>wangwu@xxx.com</email>
</user>
</list>
</parent>
对应的java类如下:
[java]
import java.util.List;
public class UserList {
private int total;
private List<User> user;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List<User> getUser() {
return user;
}
public void setUser(List<User> user) {
this.user = user;
}
}
xml解析器代码如下:
[java]
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Log;
import android.util.Xml;
/**
* 对pull解析xml进行了封装,不用给每个xml,再创建一个解析类
*
* @author steven
*
* http://www.juziku.com/sunlightcs/
*
*/
public class XmlParse {
&nbs
补充:移动开发 , Android ,