码迷,mamicode.com
首页 > 编程语言 > 详细

JavaBean转换为XML的源码

时间:2015-09-28 15:55:35      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.cmge.utils;
  2 
  3 import java.util.Iterator;
  4 
  5 import com.cmge.org.oa.bean.OADepartment;
  6 import com.coast.foundation.utils.StringUtil;
  7 import com.thoughtworks.xstream.XStream;
  8 import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
  9 import com.thoughtworks.xstream.io.xml.XppDriver;
 10 
 11 import java.util.HashMap;
 12 import java.util.LinkedList;
 13 import java.util.Map; 
 14 import java.util.Iterator; 
 15 
 16 /** 
 17 * XML与JavaBean相互转换工具类 
 18 * File: XMLBeanUtils.java 
 19 * User: leizhimin 
 20 * Date: 2008-3-5 14:28:29 
 21 */ 
 22 
 23 public class XmlXStreamUtil { 
 24     private String version = "";
 25     private String xml = "";
 26     private LinkedList<String> addXml = new LinkedList<String>();
 27     public static XmlXStreamUtil Build(){        
 28         return new XmlXStreamUtil();
 29     }
 30     
 31     public XmlXStreamUtil xmlVersion(){
 32         version = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
 33         return this;
 34     }
 35     
 36     public XmlXStreamUtil xmlVersion(String version,String encoding){
 37         version = "<?xml version=\""+version+"\" encoding=\""+encoding+"\"?>";
 38         return this;
 39     }
 40     
 41     public XmlXStreamUtil XmlAdd(String v){
 42         addXml.push(v);
 43         return this;
 44     }
 45     
 46     public String toString(){
 47         StringBuffer sb = new StringBuffer();
 48         
 49         if(!StringUtil.isNull(version))
 50             sb.append(version);
 51         for(int i = 0; i < addXml.size(); i++){
 52             String v = addXml.get(i).toString();
 53             sb.append("<"+v+">").append("\n");                
 54         }
 55         //
 56         sb.append(xml);        
 57         //        
 58         for(int i = addXml.size()-1; i >= 0; i--){
 59             String v = addXml.get(i).toString();
 60             sb.append("\n</"+v+">");
 61         }
 62         return sb.toString();
 63     }
 64     
 65     
 66     public XmlXStreamUtil bean2xml(Class cls,String alias,Object bean){
 67         bean2xml(cls,alias,null,bean);
 68         return this;
 69     }
 70     
 71     public XmlXStreamUtil bean2xml(Class cls,String alias,String attribute,Object bean){
 72         XStream xstream = new XStream(
 73                 new XppDriver(new XmlFriendlyReplacer("_-", "_"))); 
 74         xstream.alias(alias, cls);
 75         if(!StringUtil.isNull(attribute)){
 76             xstream.useAttributeFor( cls,attribute);
 77             xstream.autodetectAnnotations(true);
 78         }
 79         xml = xstream.toXML(bean);        
 80         return this;
 81     }
 82     
 83     
 84     /** 
 85      * 将Bean转换为XML 
 86      * 
 87      * @param clazzMap 别名-类名映射Map 
 88      * @param bean     要转换为xml的bean对象 
 89      * @return XML字符串 
 90      */ 
 91     public XmlXStreamUtil bean2xml(Map<String, Class> clazzMap, Object bean) { 
 92         XStream xstream = new XStream(
 93                 new XppDriver(new XmlFriendlyReplacer("_-", "_"))); 
 94         
 95         for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) { 
 96             Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next(); 
 97             System.out.println("key="+m.getKey()+",v="+m.getValue());
 98             xstream.alias(m.getKey(), m.getValue()); 
 99         }
100         xml = xstream.toXML(bean); 
101         System.out.println("xml=\n");
102         return this; 
103     } 
104 
105     /** 
106      * 将XML转换为Bean 
107      * 
108      * @param clazzMap 别名-类名映射Map 
109      * @param xml      要转换为bean对象的xml字符串 
110      * @return Java Bean对象 
111      */ 
112     public Object xml2Bean(Map<String, Class> clazzMap, String xml) { 
113         XStream xstream = new XStream(
114                 new XppDriver(new XmlFriendlyReplacer("_-", "_"))); 
115         for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) { 
116             Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next(); 
117             xstream.alias(m.getKey(), m.getValue()); 
118         } 
119         Object bean = xstream.fromXML(xml); 
120         return bean; 
121     } 
122 
123     /** 
124      * 获取XStream对象 
125      * 
126      * @param clazzMap 别名-类名映射Map 
127      * @return XStream对象 
128      */ 
129     public XStream getXStreamObject(Map<String, Class> clazzMap) { 
130         XStream xstream = new XStream(
131                 new XppDriver(new XmlFriendlyReplacer("_-", "_"))); 
132         for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) { 
133             Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next(); 
134             xstream.alias(m.getKey(), m.getValue()); 
135         } 
136         return xstream; 
137     } 
138     
139     
140     public static void main(String [] args){
141     
142         
143         OADepartment od = new OADepartment();
144         
145         od.setFullname("12312");
146         od.setCode("123");
147         od.setOrg_code("dd");
148         od.setCanceled("");
149         //XmlUtil.beanToXmlStr(OADepartment.class, "1", "2");
150         Map<String,Class> map = new HashMap();
151         map.put("org", OADepartment.class);
152         String xml = XmlXStreamUtil.Build()
153                      
154                     //    .bean2xml(map, od)
155                       .XmlAdd("root")
156                       .XmlAdd("orglist")
157                       .XmlAdd("ff")
158                       .xmlVersion()
159                        .bean2xml(OADepartment.class, "org","code", od)
160                       .toString();
161         
162         System.out.println(xml);
163         
164         
165         System.out.println("\n\n\n\n");
166         String xml2 = XmlXStreamUtil.Build()
167  
168                    .bean2xml(OADepartment.class, "org", od)
169                   .toString();
170         System.out.println(xml2);
171     }
172 }

 

JavaBean转换为XML的源码

标签:

原文地址:http://www.cnblogs.com/abc8023/p/4843956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!