java bean生成xml_dom4j读取javabean生成对应XML和读取XML得到对应的javabean对象集合

publicvoidwriteXmlDocument(T obj, ListentityPropertys, String Encode,

String XMLPathAndName) {longlasting=System.currentTimeMillis();//效率检测try{

XMLWriter writer=null;//声明写XML的对象OutputFormat format=OutputFormat.createPrettyPrint();

format.setEncoding(Encode);//设置XML文件的编码格式String filePath=XMLPathAndName;//获得文件地址File file=newFile(filePath);//获得文件if(file.exists()) {

file.delete();

}//新建student.xml文件并新增内容Document document=DocumentHelper.createDocument();

String rootname=obj.getClass().getSimpleName();//获得类名Element root=document.addElement(rootname+"s");//添加根节点Field[] properties=obj.getClass().getDeclaredFields();//获得实体类的所有属性for(T t : entityPropertys) {//递归实体Element secondRoot=root.addElement(rootname);//二级节点for(inti=0; i

.toUpperCase()+properties[i].getName().substring(1));//为二级节点添加属性,属性值为对应属性的值secondRoot.addElement(properties[i].getName()).setText(

meth.invoke(t).toString());

}

}//生成XML文件writer=newXMLWriter(newFileWriter(file), format);

writer.write(document);

writer.close();longlasting2=System.currentTimeMillis();

System.out.println("写入XML文件结束,用时"+(lasting2-lasting)+"ms");

}catch(Exception e) {

System.out.println("XML文件写入失败");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值