本文利用dom4j对一个PoJo,的属性,方法生成XML
需要的包
dom4j-1.6.1.jar
jaxen-1.1-beta-6.jar
----------------------------------------------------------------------------------------------------
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.sql.Time; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set;
import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.XMLWriter;
class MyBean { String id ; String name ; MyBean(){} MyBean(String id, String name){ this.id = id; this.name = name; } public void say(){} public String tell(String msg) throws Exception{ return msg; } }
/** * * 将java bean 互换 XML */ public class BeansToXml { public static void main(String args[]){ List l = new ArrayList(); MyBean b = new MyBean("a","b"); l.add(b); b = new MyBean("c","d"); l.add(b); createXm(b); } @SuppressWarnings("unchecked") public static void createXm(final Object obj){ String clsName = obj.getClass().getName(); clsName = clsName.substring(clsName.lastIndexOf(".")+1,clsName.length()); System.out.println(clsName); // 根据类名生成root Document doc = DocumentHelper.createDocument(); Element root = doc.addElement( clsName); root.addComment(obj.getClass().getName()+" 的xml定义"); // bean属性 Element fieldsElems = root.addElement("fields"); Class cls = obj.getClass(); Field[] fields = cls.getDeclaredFields(); String fieldName = ""; for(Field f :fields ){ f.setAccessible(true); fieldName = f.getName(); fieldsElems.addElement(fieldName); } // 方法 Element methodElems = root.addElement("methods"); Method[] methods = cls.getDeclaredMethods(); String methodName = ""; for(Method m :methods ){ methodName = m.getName(); Element methodElem = methodElems.addElement(methodName); // 生成这个方法的详细说明 createMethodElem(methodElem,m); } // 生成xml String filename ="c:/a.xml"; XMLWriter writer; try { writer = new XMLWriter(new FileWriter(new File(filename ))); writer.write(doc); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** //外部传入 java.lang.String java.lang.String java.lang.Exception, */ @SuppressWarnings("unchecked") public static void createMethodElem( Element methodElem , Method m ){ // 参数 Class[] params = m.getParameterTypes(); // 返回值 Class> returnType = m.getReturnType(); // 返回值 Class>[] exceptions = m.getExceptionTypes(); Element paramsElem = methodElem.addElement("params"); // //生成参数 StringBuffer sb = new StringBuffer(); String str = ""; Element paramType = paramsElem.addElement("param-type"); for (Class type : params){ sb.append(type.getName()).append(","); } str = sb.toString(); paramType.addText(str); // 返回值 Element returnElem = paramsElem.addElement("return-type"); sb.delete(0, sb.length()); sb.append(returnType.getName()).append(","); returnElem.addText( sb.toString()); // 异常 Element exceptionElem = paramsElem.addElement("exceptions"); sb.delete(0, sb.length()); for (Class exception : exceptions){ sb.append(exception.getName()).append(","); } exceptionElem.addText(sb.toString()); }
---------------------------------------------------------------------------XML的结果--------------------------------
java.lang.String, java.lang.Exception,