作者:fbysss
关键字:Webservice,Xml2Bean
背景:Webservice 返回的是xml,如何得到Collection类型呢?最简单的方式就是把xml还原成Collection。
准备包:commons-digester-1.8.jar
源代码1:Xml2Bean
package
cn.edu.ustb.db.task.util;
/** */
/**
* <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;
import
org.apache.commons.digester.Digester;
import
org.xml.sax.SAXException;
import
cn.edu.ustb.db.task.po.Manager;
public
class
Xml2Bean
...
{
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;
try ...{
beans = (ArrayList) digester.parse(xmlReader);
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
return beans;
}
/** *//***
* 一次性添加bean属性的Setter.否则就得一个一个的添加,很费劲。(也许digester有这个功能,但我没找到,只好自己写一个方法了)
* @param obj
* @param beanName
* @param digester
*/
private void addSetters(Object obj,String beanName, Digester digester) ...{
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) ...{
digester.addBeanPropertySetter("beans/"+beanName+"/"+field.getName());
}
/**//*digester.addBeanPropertySetter("beans/"+beanName+"/mid");
digester.addBeanPropertySetter("beans/"+beanName+"/mclass");
digester.addBeanPropertySetter("beans/"+beanName+"/mname");
*/
}
public static final void main(String args[])...{
Xml2Bean x2b = new Xml2Bean();
String xml = "<?xml versinotallow='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());
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;
public
class
Manager
implements
java.io.Serializable
...
{
// Fields
private Integer mid;
private String mname;
private Integer mclass;
// Constructors
/** *//** default constructor */
public Manager() ...{
}
/** *//** minimal constructor */
public Manager(Integer mid) ...{
this.mid = mid;
}
/** *//** full constructor */
public Manager(Integer mid,String mname, Integer mclass) ...{
this.mid = mid;
this.mname = mname;
this.mclass = mclass;
}
// Property accessors
public Integer getMid() ...{
return this.mid;
}
public void setMid(Integer mid) ...{
this.mid = mid;
}
public String getMname() ...{
return this.mname;
}
public void setMname(String mname) ...{
this.mname = mname;
}
public Integer getMclass() ...{
return this.mclass;
}
public void setMclass(Integer mclass) ...{
this.mclass = mclass;
}
}
标签:...,Webservice,beanName,mid,Collection,digester,beans,Xml2Bean,public
From: https://blog.51cto.com/u_16245757/7351671