一、前言
通过jdom的包实现JAVA及XML间相互转换的XmlReflectUtil工具类,具体包含class2xml类转xml、java对象转为xml的java2xml、xml字符串转为java对象xml2java及获取字段对象加对应xml元素getElement等。
二、代码示例
import java.io.ByteArrayInputStream;@b@import java.lang.reflect.*;@b@import java.util.*;@b@import org.jdom.Document;@b@import org.jdom.Element;@b@import org.jdom.input.SAXBuilder;@b@import org.jdom.output.Format;@b@import org.jdom.output.XMLOutputter;@b@@b@public class XmlReflectUtil@b@{@b@@b@ public XmlReflectUtil()@b@ {@b@ }@b@@b@ public static String class2xml(Class clz)@b@ {@b@ Document doc = new Document();@b@ Element root = getElement(clz);@b@ doc.setRootElement(root);@b@ XMLOutputter out = new XMLOutputter();@b@ out.setFormat(Format.getPrettyFormat());@b@ String xmlStr = out.outputString(doc);@b@ return xmlStr;@b@ }@b@@b@ public static Element getElement(Class clz)@b@ {@b@ String className = clz.getSimpleName();@b@ Element root = new Element(className);@b@ Field fields[] = clz.getDeclaredFields();@b@ Field arr$[] = fields;@b@ int len$ = arr$.length;@b@ for (int i$ = 0; i$ < len$; i$++)@b@ {@b@ Field field = arr$[i$];@b@ root.addContent(getElement(field));@b@ }@b@@b@ return root;@b@ }@b@@b@ public static Element getElement(Field field)@b@ {@b@ if ((field.getModifiers() & 0x80) != 0)@b@ return null;@b@ String fieldName = field.getName();@b@ Element el = new Element(fieldName);@b@ Class type = field.getType();@b@ if (type != java.lang.Integer.class && type != java.lang.Integer.class)@b@ if (type == java.util.List.class)@b@ {@b@ Class genericType = (Class)((ParameterizedType)field.getGenericType()).getActualTypeArguments()[0];@b@ el.addContent(getElement(genericType));@b@ } else@b@ {@b@ getElement(type);@b@ }@b@ return el;@b@ }@b@@b@ public static Element getElement(Object obj, String elementName)@b@ throws Exception@b@ {@b@ if (elementName.equalsIgnoreCase("String"))@b@ {@b@ Element el = new Element("String");@b@ el.setText((String)obj);@b@ return el;@b@ }@b@ Class clz = obj.getClass();@b@ Element root = new Element(elementName);@b@ Field fields[] = clz.getDeclaredFields();@b@ Field arr$[] = fields;@b@ int len$ = arr$.length;@b@ for (int i$ = 0; i$ < len$; i$++)@b@ {@b@ Field field = arr$[i$];@b@ if ((field.getModifiers() & 0x80) != 0)@b@ continue;@b@ String fieldName = field.getName();@b@ Method method = clz.getDeclaredMethod((new StringBuilder()).append("get").append(fieldName).toString(), new Class[0]);@b@ Object value = method.invoke(obj, new Object[0]);@b@ Element el = null;@b@ Class type = field.getType();@b@ if (value == null)@b@ el = new Element(fieldName);@b@ else@b@ if (type == java.lang.Integer.class || type == java.lang.String.class)@b@ {@b@ el = new Element(fieldName);@b@ el.setText(value.toString());@b@ } else@b@ if (type == java.util.List.class)@b@ {@b@ el = new Element(fieldName);@b@ List list = (List)value;@b@ Object subObj;@b@ for (Iterator ii$ = list.iterator(); ii$.hasNext(); el.addContent(getElement(subObj, subObj.getClass().getSimpleName())))@b@ subObj = ii$.next();@b@@b@ } else@b@ {@b@ getElement(value, fieldName);@b@ }@b@ root.addContent(el);@b@ }@b@@b@ return root;@b@ }@b@@b@ public static String java2xml(Object obj)@b@ throws Exception@b@ {@b@ Document doc = new Document();@b@ Element root = getElement(obj, obj.getClass().getSimpleName());@b@ doc.setRootElement(root);@b@ XMLOutputter out = new XMLOutputter();@b@ out.setFormat(Format.getPrettyFormat());@b@ String xmlStr = out.outputString(doc);@b@ return xmlStr;@b@ }@b@@b@ public static Object xml2java(String xml, Class clz)@b@ throws Exception@b@ {@b@ SAXBuilder builder = new SAXBuilder();@b@ Document doc = builder.build(new ByteArrayInputStream(xml.getBytes("UTF-8")));@b@ Element root = doc.getRootElement();@b@ if (!root.getName().equalsIgnoreCase(clz.getSimpleName()))@b@ throw new Exception((new StringBuilder()).append("xml not match class[ xml root:").append(root.getName()).append(", clz:").append(clz.getSimpleName()).append("]").toString());@b@ Object obj = clz.newInstance();@b@ Field fields[] = clz.getDeclaredFields();@b@ Field arr$[] = fields;@b@ int len$ = arr$.length;@b@ for (int i$ = 0; i$ < len$; i$++)@b@ {@b@ Field field = arr$[i$];@b@ if ((field.getModifiers() & 0x80) != 0)@b@ continue;@b@ Element el = root.getChild(field.getName());@b@ if (el == null)@b@ continue;@b@ String text = el.getText();@b@ Method method = clz.getDeclaredMethod((new StringBuilder()).append("set").append(field.getName()).toString(), new Class[] {@b@ field.getType()@b@ });@b@ if (field.getType() == java.lang.String.class)@b@ {@b@ method.invoke(obj, new Object[] {@b@ text@b@ });@b@ continue;@b@ }@b@ if (field.getType() != java.lang.Integer.class)@b@ continue;@b@ try@b@ {@b@ Integer num = new Integer(text);@b@ method.invoke(obj, new Object[] {@b@ num@b@ });@b@ }@b@ catch (NumberFormatException e)@b@ {@b@ method.invoke(obj, new Object[] {@b@ null@b@ });@b@ }@b@ }@b@@b@ return obj;@b@ }@b@@b@ public static Object xml2java(Element root, Class clz)@b@ throws Exception@b@ {@b@ Object obj = clz.newInstance();@b@ Field fields[] = clz.getDeclaredFields();@b@ Field arr$[] = fields;@b@ int len$ = arr$.length;@b@ for (int i$ = 0; i$ < len$; i$++)@b@ {@b@ Field field = arr$[i$];@b@ if ((field.getModifiers() & 0x80) != 0)@b@ continue;@b@ Method method = clz.getDeclaredMethod((new StringBuilder()).append("set").append(field.getName()).toString(), new Class[] {@b@ field.getType()@b@ });@b@ if (field.getType() == java.lang.String.class)@b@ {@b@ Element el = root.getChild(field.getName());@b@ if (el != null)@b@ {@b@ String text = el.getText();@b@ method.invoke(obj, new Object[] {@b@ text@b@ });@b@ }@b@ continue;@b@ }@b@ if (field.getType() == java.lang.Integer.class)@b@ {@b@ Element el = root.getChild(field.getName());@b@ if (el == null)@b@ continue;@b@ String text = el.getText();@b@ try@b@ {@b@ Integer num = new Integer(text);@b@ method.invoke(obj, new Object[] {@b@ num@b@ });@b@ }@b@ catch (NumberFormatException e)@b@ {@b@ method.invoke(obj, new Object[] {@b@ null@b@ });@b@ }@b@ continue;@b@ }@b@ if (field.getType() != java.util.List.class)@b@ continue;@b@ Class genericType = (Class)((ParameterizedType)field.getGenericType()).getActualTypeArguments()[0];@b@ List list = new ArrayList();@b@ List els = null;@b@ Element listEle = root.getChild(field.getName());@b@ if (listEle != null)@b@ els = listEle.getChildren(genericType.getSimpleName());@b@ else@b@ els = root.getChildren(genericType.getSimpleName());@b@ Element element;@b@ for (Iterator it$ = els.iterator(); it$.hasNext(); list.add(xml2java(element, genericType)))@b@ element = (Element)it$.next();@b@@b@ method.invoke(obj, new Object[] {@b@ list@b@ });@b@ }@b@@b@ return obj;@b@ }@b@}