import java.util.Iterator;
import java.util.Map;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
import com.thoughtworks.xstream.io.xml.XppDriver;
@SuppressWarnings("deprecation")
public class XMLBeanUtils {
/**
* 将Bean转换为XML
*
* @param clazzMap
* 别名-类名映射Map
* @param bean
* 要转换为xml的bean对象
* @return XML字符串
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String 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();
xstream.alias(m.getKey(), m.getValue());
}
String xml = xstream.toXML(bean);
return xml;
}
/**
* 将XML转换为Bean
*
* @param clazzMap
* 别名-类名映射Map
* @param xml
* 要转换为bean对象的xml字符串
* @return Java Bean对象
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object xml2Bean(Map<String, Class> clazzMap, String xml) {
XStream xstream = new XStream(new DomDriver());
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对象
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static XStream getXStreamObject(Map<String, Class> clazzMap) {
XStream xstream = new XStream(new DomDriver());
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;
}
}
import java.util.Map;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
import com.thoughtworks.xstream.io.xml.XppDriver;
@SuppressWarnings("deprecation")
public class XMLBeanUtils {
/**
* 将Bean转换为XML
*
* @param clazzMap
* 别名-类名映射Map
* @param bean
* 要转换为xml的bean对象
* @return XML字符串
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String 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();
xstream.alias(m.getKey(), m.getValue());
}
String xml = xstream.toXML(bean);
return xml;
}
/**
* 将XML转换为Bean
*
* @param clazzMap
* 别名-类名映射Map
* @param xml
* 要转换为bean对象的xml字符串
* @return Java Bean对象
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object xml2Bean(Map<String, Class> clazzMap, String xml) {
XStream xstream = new XStream(new DomDriver());
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对象
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static XStream getXStreamObject(Map<String, Class> clazzMap) {
XStream xstream = new XStream(new DomDriver());
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;
}
}