将bean对象写成xml格式

来源:互联网 发布:语音播报软件 编辑:程序博客网 时间:2024/06/14 10:43

首先创建两个内部bean组件,设置get,set方法

class TestBean extends TestBean1{private String att1;private String att2;private int int1;private List<TestBean1> listBean;public List<TestBean1> getListBean() {return listBean;}public void setListBean(List<TestBean1> listBean) {this.listBean = listBean;}public String getAtt1() {return att1;}public void setAtt1(String att1) {this.att1 = att1;}public String getAtt2() {return att2;}public void setAtt2(String att2) {this.att2 = att2;}public int getInt1() {return int1;}public void setInt1(int int1) {this.int1 = int1;}}class TestBean1{private String aaa;private String bbb;public String getAaa() {return aaa;}public void setAaa(String aaa) {this.aaa = aaa;}public String getBbb() {return bbb;}public void setBbb(String bbb) {this.bbb = bbb;}
xml的根元素应该是bean的名称,所以我们要获取到bean的名称

public static String getClassNameWithoutPackage(Class cl)  {    String className = cl.getName();    int pos = className.lastIndexOf('.') + 1;//将包名截取调    if (pos == -1) {      pos = 0;    }    return className.substring(pos);  }
创建xml

public static byte[] toBytes(Object bean, String encoding) throws Exception {Document document = DocumentHelper.createDocument();//创建Element root = document.addElement(getClassNameWithoutPackage(bean.getClass);//添加根元素addNode(root, bean);//添加node节点// document.setXMLEncoding(encoding);
private static byte[] toBytes(Document document, String encoding)throws Exception {OutputFormat format = new OutputFormat("\t", true);format.setEncoding(encoding);ByteArrayOutputStream out = new ByteArrayOutputStream();XMLWriter writer = new XMLWriter(out, format);writer.write(document);return out.toByteArray();

private static void addNode(Element e, Object bean) throws Exception {List fieldsName = ObjectUtils.getAllFieldName(bean.getClass());// 获取所有属性名称int len = fieldsName.size();for (int i = 0; i < len; i++) {String fieldName = (String) fieldsName.get(i);Object o = DtoUtils.getFieldValue(bean, fieldName);// 获取属性对应的值if (o == null) {} else if (o instanceof String) {String fieldValue = (String) o;e.addElement(fieldName).addText(fieldValue);} else if (o instanceof List) {List l = (List) o;for (Iterator it = l.iterator(); it.hasNext();) {Object lo = it.next();Element oe = e.addElement(DtoUtils.getBeanName(lo));addNode(oe, lo);}} else {// 如果是对象Element t = e.addElement(DtoUtils.getBeanName(o));addNode(t, o);}}}
获取所有属性名称的方法

public static List getAllFieldName(Class cl) {    List list = new ArrayList();    Field[] fields = cl.getDeclaredFields();    for (int i = 0; i < fields.length; i++) {      Field field = fields[i];      if (!field.getName().equals("serialVersionUID"))//序列号      {        list.add(field.getName());      }    }    Class superClass = cl;    while (true) {      superClass = superClass.getSuperclass();      if (superClass == Object.class) {        break;      }      list.addAll(getAllFieldName(superClass));    }    return list;  }
获取属性对应的值的方法

public static Object getFieldValue(Object bean, String fieldName)throws Exception {String uFieldName = fieldName.substring(0, 1).toUpperCase(Locale.US) + fieldName.substring(1);//将第一个字母变为大写Object o = null;try {o = ObjectUtils.invoke(bean, "get" + uFieldName, null, null);} catch (java.lang.NoSuchMethodException nsme) {//   nsme.printStackTrace();}return o;}

public static Object invoke(Object oldObject, String methodName, Class[] argsClass, Object[] args)    throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException  {    Class cl = oldObject.getClass();    Method method = cl.getMethod(methodName, argsClass);    return method.invoke(oldObject, args);  }

写出操作

private static byte[] toBytes(Document document, String encoding)throws Exception {OutputFormat format = new OutputFormat("\t", true);format.setEncoding(encoding);ByteArrayOutputStream out = new ByteArrayOutputStream();XMLWriter writer = new XMLWriter(out, format);writer.write(document);return out.toByteArray();






1 0