XML与JavaBean相互转换工具
生活随笔
收集整理的這篇文章主要介紹了
XML与JavaBean相互转换工具
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
XML與JavaBean相互轉換工具 import com.thoughtworks.xstream.XStream;
import java.util.Map;
import java.util.Iterator;
/**
* XML與JavaBean相互轉換工具類
* File: XMLBeanUtils.java
* User: leizhimin
* Date: 2008-3-5 14:28:29
*/
public final class XMLBeanUtils {
????/**
???? * 將Bean轉換為XML
???? *
???? * @param clazzMap 別名-類名映射Map
???? * @param bean???? 要轉換為xml的bean對象
???? * @return XML字符串
???? */
????public static String bean2xml(Map<String, Class> clazzMap, Object bean) {
????????XStream xstream = new XStream();
????????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對象
???? */
????public static Object xml2Bean(Map<String, Class> clazzMap, String xml) {
????????XStream xstream = new XStream();
????????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 static XStream getXStreamObject(Map<String, Class> clazzMap) {
????????XStream xstream = new XStream();
????????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 com.thoughtworks.xstream.XStream;
import java.util.Map;
import java.util.HashMap;
/**
* Created by IntelliJ IDEA.
* File: XStreamFactory.java
* User: leizhimin
* Date: 2008-3-6 15:36:13
*/
public class XStreamFactory {
????/**
???? * 獲取????的XStream對象
???? *
???? * @return XStream對象
???? */
????public static XStream getXXXStream() {
????????Map<String, Class> cm = new HashMap<String, Class>();
????????cm.put("test", org.xxx.Test.class);
????????cm.put("ssss", org.xxx.s.Ssss.class);
????????return XMLBeanUtils.getXStreamObject(cm);
????}
}
import java.util.Map;
import java.util.Iterator;
/**
* XML與JavaBean相互轉換工具類
* File: XMLBeanUtils.java
* User: leizhimin
* Date: 2008-3-5 14:28:29
*/
public final class XMLBeanUtils {
????/**
???? * 將Bean轉換為XML
???? *
???? * @param clazzMap 別名-類名映射Map
???? * @param bean???? 要轉換為xml的bean對象
???? * @return XML字符串
???? */
????public static String bean2xml(Map<String, Class> clazzMap, Object bean) {
????????XStream xstream = new XStream();
????????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對象
???? */
????public static Object xml2Bean(Map<String, Class> clazzMap, String xml) {
????????XStream xstream = new XStream();
????????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 static XStream getXStreamObject(Map<String, Class> clazzMap) {
????????XStream xstream = new XStream();
????????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 com.thoughtworks.xstream.XStream;
import java.util.Map;
import java.util.HashMap;
/**
* Created by IntelliJ IDEA.
* File: XStreamFactory.java
* User: leizhimin
* Date: 2008-3-6 15:36:13
*/
public class XStreamFactory {
????/**
???? * 獲取????的XStream對象
???? *
???? * @return XStream對象
???? */
????public static XStream getXXXStream() {
????????Map<String, Class> cm = new HashMap<String, Class>();
????????cm.put("test", org.xxx.Test.class);
????????cm.put("ssss", org.xxx.s.Ssss.class);
????????return XMLBeanUtils.getXStreamObject(cm);
????}
}
總結
以上是生活随笔為你收集整理的XML与JavaBean相互转换工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《解剖PetShop》系列之二
- 下一篇: 杀毒软件全免费遭厂家“抵制”