JavaBean转换为XML的源码

时间:2023-03-08 15:51:36
 package com.cmge.utils;

 import java.util.Iterator;

 import com.cmge.org.oa.bean.OADepartment;
import com.coast.foundation.utils.StringUtil;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
import com.thoughtworks.xstream.io.xml.XppDriver; import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Iterator; /**
* XML与JavaBean相互转换工具类
* File: XMLBeanUtils.java
19
*/

public class XmlXStreamUtil {
private String version = "";
private String xml = "";
private LinkedList<String> addXml = new LinkedList<String>();
public static XmlXStreamUtil Build(){
return new XmlXStreamUtil();
} public XmlXStreamUtil xmlVersion(){
version = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
return this;
} public XmlXStreamUtil xmlVersion(String version,String encoding){
version = "<?xml version=\""+version+"\" encoding=\""+encoding+"\"?>";
return this;
} public XmlXStreamUtil XmlAdd(String v){
addXml.push(v);
return this;
} public String toString(){
StringBuffer sb = new StringBuffer(); if(!StringUtil.isNull(version))
sb.append(version);
for(int i = 0; i < addXml.size(); i++){
String v = addXml.get(i).toString();
sb.append("<"+v+">").append("\n");
}
//
sb.append(xml);
//
for(int i = addXml.size()-1; i >= 0; i--){
String v = addXml.get(i).toString();
sb.append("\n</"+v+">");
}
return sb.toString();
} public XmlXStreamUtil bean2xml(Class cls,String alias,Object bean){
bean2xml(cls,alias,null,bean);
return this;
} public XmlXStreamUtil bean2xml(Class cls,String alias,String attribute,Object bean){
XStream xstream = new XStream(
new XppDriver(new XmlFriendlyReplacer("_-", "_")));
xstream.alias(alias, cls);
if(!StringUtil.isNull(attribute)){
xstream.useAttributeFor( cls,attribute);
xstream.autodetectAnnotations(true);
}
xml = xstream.toXML(bean);
return this;
} /**
* 将Bean转换为XML
*
* @param clazzMap 别名-类名映射Map
* @param bean 要转换为xml的bean对象
* @return XML字符串
*/
public XmlXStreamUtil bean2xml(Map<String, Class> clazzMap, Object bean) {
XStream xstream = new XStream(
new XppDriver(new XmlFriendlyReplacer("_-", "_"))); for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next();
System.out.println("key="+m.getKey()+",v="+m.getValue());
xstream.alias(m.getKey(), m.getValue());
}
xml = xstream.toXML(bean);
System.out.println("xml=\n");
return this;
} /**
* 将XML转换为Bean
*
* @param clazzMap 别名-类名映射Map
* @param xml 要转换为bean对象的xml字符串
* @return Java Bean对象
*/
public Object xml2Bean(Map<String, Class> clazzMap, String xml) {
XStream xstream = new XStream(
new XppDriver(new XmlFriendlyReplacer("_-", "_")));
for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next();
xstream.alias(m.getKey(), m.getValue());
}
Object bean = xstream.fromXML(xml);
return bean;
} /**
* 获取XStream对象
*
* @param clazzMap 别名-类名映射Map
* @return XStream对象
*/
public XStream getXStreamObject(Map<String, Class> clazzMap) {
XStream xstream = new XStream(
new XppDriver(new XmlFriendlyReplacer("_-", "_")));
for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next();
xstream.alias(m.getKey(), m.getValue());
}
return xstream;
} public static void main(String [] args){ OADepartment od = new OADepartment(); od.setFullname("12312");
od.setCode("123");
od.setOrg_code("dd");
od.setCanceled("");
//XmlUtil.beanToXmlStr(OADepartment.class, "1", "2");
Map<String,Class> map = new HashMap();
map.put("org", OADepartment.class);
String xml = XmlXStreamUtil.Build() // .bean2xml(map, od)
.XmlAdd("root")
.XmlAdd("orglist")
.XmlAdd("ff")
.xmlVersion()
.bean2xml(OADepartment.class, "org","code", od)
.toString(); System.out.println(xml); System.out.println("\n\n\n\n");
String xml2 = XmlXStreamUtil.Build() .bean2xml(OADepartment.class, "org", od)
.toString();
System.out.println(xml2);
}
}