如何实现任意对象转换为XML格式
求实现将任意对象(MapToXML,ArrayListToXML,collectionToXML,BeanToXML等)转换为XML格式字符串的方法或工具
如,
Map userMap=new HashMap();
userMap.put("username","test");
userMap.put("sex","1");
UserMap.put("collection",myArrayList);
XMLUtil.toXML(userMap,"User");
得到的结果:
<User>
<username>test</username>
<sex>1</sex>
<collection>
//集合内容
……
</collection>
</User>
当然如果复杂对象里包含着集合也可以正确转换为XML,如有这类的开源工具也可
(xstream 可以实现javabean to xml的转换,但是没办法用Map ,ArrayList转换为如上格式的数据)