将bean对象写成xml格式

首先创建两个内部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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值