安卓快速解析xml工具类
xml工具类XmlHelper
利用sax的DefaultHandler做快速的解析,并通过反射把解析结果储存在一个对象里或者一个储存该对象的list里。
XmlHelper代码
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XmlHelper {
private static XmlHelper util;
public static XmlHelper getInstance() {
if (util == null) {
util = new XmlHelper();
}
return util;
}
private XmlHelper() {
super();
}
public <T> List<T> getList(Class<T> c, String str, String matchesNode) {
ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes());
List<T> list = getList(c, in, matchesNode);
return list;
}
public <T> T getObject(Class<T> c, String str, String matchesNode) {
ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes());
T obj = getObject(c, in, matchesNode);
return obj;
}
public <T> List<T> getList(Class<T> c, InputStream in, String matchesNode) {
XMLListHandle<T> handle = new XMLListHandle<T>(c, in, matchesNode);
List<T> list = handle.getList();
return list;
}
public <T> T getObject(Class<T> c, InputStream in, String matchesNode) {
XMLListHandle<T> handle = new XMLListHandle<T>(c, in, matchesNode);
T obj = (T) handle.getObject();
return obj;
}
private class XMLListHandle<T> extends DefaultHandler {
private T obj;
private Stack<String> stack = new Stack<String>();
private Class<T> c = null;
private Field[] fields = null;
private List<T> list = new ArrayList<T>();
private String matchesNode;
public XMLListHandle(Class<T> c, InputStream in, String matchesNode) {
this.matchesNode = matchesNode;
this.c = c;
this.fields = c.getDeclaredFields();
try {
this.obj = c.newInstance();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
SAXParser saxParser = spf.newSAXParser();
InputSource is = new InputSource(new InputStreamReader(in,
"UTF-8"));
saxParser.parse(is, this);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes attr) throws SAXException {
stack.push(qName);
try {
if (qName.equalsIgnoreCase(matchesNode)) {
this.obj = c.newInstance();
}
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
for (int i = 0; i < attr.getLength(); i++) {
for (Field field : fields) {
String aQName = attr.getQName(i);
String aLName = attr.getLocalName(i);
String fName = field.getName();
if (fName.equalsIgnoreCase(aQName)
|| fName.equalsIgnoreCase(aLName)) {
try {
field.setAccessible(true);
if (obj != null) {
field.set(obj, attr.getValue(aQName));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
@Override
public void characters(char[] ch, int start, int end)
throws SAXException {
String tag = stack.peek();
for (Field field : fields) {
if (tag.equalsIgnoreCase(field.getName())) {
String string = new String(ch, start, end);
try {
field.setAccessible(true);
if (obj != null) {
field.set(obj, string);
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
String tag = stack.peek();
if (tag.equalsIgnoreCase(matchesNode) && obj != null) {
list.add(obj);
}
stack.pop();
}
public List<T> getList() {
return list;
}
public T getObject() {
return obj;
}
}
}
使用方法
String result = "<root><obj name=‘name‘ age=‘10‘/> <obj name=‘name1‘ age=‘20‘/></root>";
Bean bean = XmlHelper.getInstance().getObject(Bean.class, result,
"root");
List<Bean> list = XmlHelper.getInstance().getList(Bean.class, result,
"obj");
System.out.println(bean.toString());
for (Bean bean2 : list) {
System.out.println(bean2.toString());
}
Bean.java
public class Bean {
String name="";
String age="";
@Override
public String toString() {
return "Bean [name=" + name + ", age=" + age + "]";
}
}
打印结果
03-10 13:25:01.571: I/System.out(21465): Bean [name=name1, age=20]
03-10 13:25:01.571: I/System.out(21465): Bean [name=name, age=10]
03-10 13:25:01.571: I/System.out(21465): Bean [name=name1, age=20]
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。