作者:fbysss
msn:jameslastchina@hotmail.com
blog:blog.csdn.net/fbysss
声明:本文由fbysss原创,转载请注明出处
关键字:Webservice,Xml2Bean
背景:Webservice 返回的是xml,如何得到Collection类型呢?最简单的方式就是把xml还原成Collection。
准备包:commons-digester-1.8.jar
源代码1:Xml2Bean
package
cn.edu.ustb.db.task.util;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* <p>标题: Xml2Bean.java</p>
* <p>功能描述: 用于将xml转换成bean<br>
*
* </p>
* <p>版权: Copyright (c) 2007</p>
* <p>公司: </p>
* @author fbysss
* @version 1.0
*
* </p>
* <p>修改记录:</p>
* 创建时间:2007-6-23 13:19:39
* 类说明:要求jdk1.5版本以上。
*/
import
java.io.IOException;
import
java.io.StringReader;
import
java.lang.reflect.Field;
import
java.util.ArrayList;
import
java.util.List;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.apache.commons.digester.Digester;
import
org.xml.sax.SAXException;
import
cn.edu.ustb.db.task.po.Manager;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Xml2Bean
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public List getObjects(String xml,Object obj)...{
String beanName = obj.getClass().getSimpleName();
StringReader xmlReader = new StringReader(xml);
Digester digester = new Digester();
digester.setValidating(true);
digester.addObjectCreate("beans", ArrayList.class);
digester.addObjectCreate("beans/"+beanName ,obj.getClass());
digester.addSetProperties("beans/"+beanName);
addSetters(obj, beanName, digester);
digester.addSetNext("beans/"+beanName, "add");
ArrayList beans = null;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
beans = (ArrayList) digester.parse(xmlReader);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (SAXException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
return beans;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//***
* 一次性添加bean属性的Setter.否则就得一个一个的添加,很费劲。(也许digester有这个功能,但我没找到,只好自己写一个方法了)
* @param obj
* @param beanName
* @param digester
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void addSetters(Object obj,String beanName, Digester digester) ...{
Field[] fields = obj.getClass().getDeclaredFields();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (Field field : fields) ...{
digester.addBeanPropertySetter("beans/"+beanName+"/"+field.getName());
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*digester.addBeanPropertySetter("beans/"+beanName+"/mid");
digester.addBeanPropertySetter("beans/"+beanName+"/mclass");
digester.addBeanPropertySetter("beans/"+beanName+"/mname");
*/
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static final void main(String args[])...{
Xml2Bean x2b = new Xml2Bean();
String xml = "<?xml version='1.0' ?> <beans><Manager> <mclass>1</mclass> <mid>1</mid><mname>sss</mname></Manager> <Manager><mclass>2</mclass><mid>1</mid><mname>sss2</mname></Manager></beans>";
List lst = x2b.getObjects(xml, new Manager());
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (Object object : lst) ...{
Manager manager = (Manager) object;
System.out.println("id:"+manager.getMid()+",name:"+manager.getMname());
}
}
}
源代码2:Manager.java
package
cn.edu.ustb.db.task.po;
import
java.util.HashSet;
import
java.util.Set;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Manager
implements
java.io.Serializable
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Fields
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private Integer mid;
private String mname;
private Integer mclass;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Constructors
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** default constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Manager() ...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** minimal constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Manager(Integer mid) ...{
this.mid = mid;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** full constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Manager(Integer mid,String mname, Integer mclass) ...{
this.mid = mid;
this.mname = mname;
this.mclass = mclass;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Property accessors
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Integer getMid() ...{
return this.mid;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setMid(Integer mid) ...{
this.mid = mid;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getMname() ...{
return this.mname;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setMname(String mname) ...{
this.mname = mname;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Integer getMclass() ...{
return this.mclass;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setMclass(Integer mclass) ...{
this.mclass = mclass;
}
}