Android XML小工具
一、概述
XML与Object互相转化,支持8种基本类型、String和byte[]。byte[]需要经由base64编码(API8以后才有,之前版本可以自己拷入)。
更多代码及测试用例,见附件工程!
二、Object –> XML
- public class XmlBuilder {
-
-
-
-
-
-
-
-
- public void buildXml(OutputStream outs, String encoding, Object obj)
- throws IllegalArgumentException, IllegalStateException,
- IOException, IllegalAccessException {
- XmlSerializer serializer = Xml.newSerializer();
- serializer.setOutput(outs, encoding);
- serializer.startDocument(encoding, true);
- writeXml(serializer, obj);
- serializer.endDocument();
- outs.close();
- }
-
-
-
-
-
-
-
- private void writeXml(XmlSerializer serializer, Object obj)
- throws IllegalArgumentException, IllegalStateException,
- IOException, IllegalAccessException {
-
- final String tag_type = "type";
- final String tag_value = "value";
-
- Class<?> cls = obj.getClass();
- String clsName = cls.getName();
-
- serializer.startTag(null, clsName);
-
- Field[] fields = cls.getDeclaredFields();
-
- boolean isWrite;
- Class<?> type;
- String varName, typeName, value = null;
- for (Field field : fields) {
- isWrite = true;
- type = field.getType();
- typeName = type.getName();
- field.setAccessible(true);
- if (type.isPrimitive()) {
- if (typeName.equals("boolean")) {
- value = String.valueOf(field.getBoolean(obj));
- } else if (typeName.equals("char")) {
-
- char c = field.getChar(obj);
- value = c == '\u0000' ? "null" : String.valueOf(field
- .getChar(obj));
- } else if (typeName.equals("byte")) {
- value = String.valueOf(field.getByte(obj));
- } else if (typeName.equals("short")) {
- value = String.valueOf(field.getShort(obj));
- } else if (typeName.equals("int")) {
- value = String.valueOf(field.getInt(obj));
- } else if (typeName.equals("long")) {
- value = String.valueOf(field.getLong(obj));
- } else if (typeName.equals("float")) {
- value = String.valueOf(field.getFloat(obj));
- } else if (typeName.equals("double")) {
- value = String.valueOf(field.getDouble(obj));
- }
- } else if (typeName.equals("java.lang.String")) {
- value = field.get(obj) == null ? "null" : field.get(obj)
- .toString();
- } else if (typeName.equals("[B")) {
- typeName = getTypeHelper(type);
-
- value = field.get(obj) == null ? "null"
- : Base64.encodeToString((byte[]) field.get(obj),
- Base64.DEFAULT);
- } else {
- isWrite = false;
- }
- if (isWrite) {
- varName = field.getName();
- serializer.startTag(null, varName);
- writeTag(serializer, tag_type, typeName);
- writeTag(serializer, tag_value, value);
- serializer.endTag(null, varName);
- }
- }
-
- serializer.endTag(null, clsName);
- }
-
-
-
-
- private String getTypeHelper(Class<?> type) {
- if (type.isArray()) {
- Class<?> c = type.getComponentType();
- return getTypeHelper(c) + "[]";
- } else {
- return type.getName();
- }
- }
-
-
-
-
- private void writeTag(XmlSerializer serializer, String tag, String value)
- throws IllegalArgumentException, IllegalStateException, IOException {
- serializer.startTag(null, tag);
- serializer.text(value);
- serializer.endTag(null, tag);
- }
-
- }
三、XML -> Object
- public class XmlParser {
-
-
-
-
-
-
-
-
- public void parseXml(InputStream ins, String encoding, Object obj)
- throws XmlPullParserException, IllegalArgumentException,
- UnsupportedEncodingException, IllegalAccessException, IOException {
- XmlPullParser parser = Xml.newPullParser();
- parser.setInput(ins, encoding);
- parseXml(parser, obj);
- ins.close();
- }
-
-
-
-
-
-
-
- private void parseXml(XmlPullParser parser, Object obj)
- throws XmlPullParserException, IllegalArgumentException,
- UnsupportedEncodingException, IllegalAccessException, IOException {
-
- final String tag_value = "value";
-
- Class<?> cls = obj.getClass();
- String clsName = cls.getName();
-
- Field[] fields = cls.getDeclaredFields();
-
- Field field = null;
-
- int tagDepth;
- String tagName;
- int eventType = parser.getEventType();
- while (true) {
- if (eventType == XmlPullParser.START_DOCUMENT) {
-
- } else if (eventType == XmlPullParser.START_TAG) {
-
-
- tagDepth = parser.getDepth();
- tagName = parser.getName();
-
- if (tagDepth == 1) {
- if (!tagName.equals(clsName)) {
- throw new XmlPullParserException("XML首标签名与对象名称不一致");
- }
- } else if (tagDepth == 2) {
-
- field = hasField(tagName, fields);
- } else if (tagDepth == 3 && field != null) {
- if (tagName.equals(tag_value)) {
- setValue(obj, field, parser.nextText());
- field = null;
- }
- }
- } else if (eventType == XmlPullParser.END_TAG) {
-
- } else if (eventType == XmlPullParser.TEXT) {
-
- } else if (eventType == XmlPullParser.END_DOCUMENT) {
-
- break;
- }
- eventType = parser.next();
- }
- }
-
-
-
-
-
-
-
-
- private Field hasField(String tagName, Field[] fields) {
- for (Field field : fields) {
- if (tagName.equals(field.getName())) {
- return field;
- }
- }
- return null;
- }
-
-
-
-
-
-
-
-
- private void setValue(Object obj, Field field, String value)
- throws IllegalArgumentException, IllegalAccessException,
- UnsupportedEncodingException {
- Class<?> type = field.getType();
- String typeName = type.getName();
- field.setAccessible(true);
- if (type.isPrimitive()) {
- if (typeName.equals("boolean")) {
- field.setBoolean(obj, Boolean.parseBoolean(value));
- } else if (typeName.equals("char")) {
- if (!value.equals("null")) {
- field.setChar(obj, value.charAt(0));
- }
- } else if (typeName.equals("byte")) {
- field.setByte(obj, Byte.parseByte(value));
- } else if (typeName.equals("short")) {
- field.setShort(obj, Short.parseShort(value));
- } else if (typeName.equals("int")) {
- field.setInt(obj, Integer.parseInt(value));
- } else if (typeName.equals("long")) {
- field.setLong(obj, Long.parseLong(value));
- } else if (typeName.equals("float")) {
- field.setFloat(obj, Float.parseFloat(value));
- } else if (typeName.equals("double")) {
- field.setDouble(obj, Double.parseDouble(value));
- }
- } else if (typeName.equals("java.lang.String")) {
- if (!value.equals("null")) {
- field.set(obj, value);
- }
- } else if (typeName.equals("[B")) {
- if (!value.equals("null")) {
-
- field.set(obj, Base64.decode(value, Base64.DEFAULT));
- }
- }
- }
-
- }
四、后记
Json与Object互相转化,可看《
Android实时绘制效果》附件工程内的JsonUtil。