package com.huawei.t0901_betwixt;
import java.beans.IntrospectionException;
import java.io.File;
import java.io.IOException;
import org.apache.commons.betwixt.io.BeanReader;
import org.xml.sax.SAXException;
public class TestMain
{
// 请求文件地址
private static final String PATH = "src/userReq.xml";
// 实体类
private static final String BEAN_CLASS = "com.huawei.t0901_betwixt.UserReq";
public static void main(String[] args)
throws ClassNotFoundException, IntrospectionException, IOException, SAXException
{
BeanReader reader = new BeanReader();
Class clazz = Class.forName(BEAN_CLASS);
UserReq uq = (UserReq) parseXml(clazz,reader);
for (User u : uq.getUser())
{
System.out.println(u + "\n-----------");
}
}
private static Object parseXml(Class clazz, BeanReader reader)
throws IntrospectionException, IOException, SAXException
{
// 注册
reader.registerBeanClass(clazz);
// 解析xml
return reader.parse(new File(PATH));
}
}
------------------------User-------------------------
package com.huawei.t0901_betwixt;
public class User
{
// 姓名
private String name = "--";
// 电话
private String tel;
// 性别
private String sex;
// 地址
private String address;
/**
* 默认构造函数
*/
public User()
{
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getTel()
{
return tel;
}
public void setTel(String tel)
{
this.tel = tel;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
@Override
public String toString()
{
return "姓名:" + name + "\n性别:" + (sex.equalsIgnoreCase("M") ? "男" : "女")
+ "\n联系电话:" + tel + "\n家庭地址:" + address;
}
}
--------------------UserReq----------------------
package com.huawei.t0901_betwixt;
import java.util.ArrayList;
import java.util.List;
/**
* 用户请求
*
* @author chenglia
* @version [版本号, 2011-9-1]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class UserReq
{
// 用户
private List<User> user = new ArrayList<User>();
/**
* 默认构造函数
*/
public UserReq()
{
}
public List<User> getUser()
{
return user;
}
public void setUser(List<User> user)
{
this.user = user;
}
public void addUser(User u)
{
user.add(u);
}
}
--------------------userReq.bewixt-----------
<info primitiveTypes="element">
<element name="userReq">
<element name="user" property="user" updater="addUser" />
<addDefaults/>
</element>
</info>
----------------------user.bewixt-------------
<info primitiveTypes="element">
<element name="user">
<addDefaults/>
</element>
</info>
import java.beans.IntrospectionException;
import java.io.File;
import java.io.IOException;
import org.apache.commons.betwixt.io.BeanReader;
import org.xml.sax.SAXException;
public class TestMain
{
// 请求文件地址
private static final String PATH = "src/userReq.xml";
// 实体类
private static final String BEAN_CLASS = "com.huawei.t0901_betwixt.UserReq";
public static void main(String[] args)
throws ClassNotFoundException, IntrospectionException, IOException, SAXException
{
BeanReader reader = new BeanReader();
Class clazz = Class.forName(BEAN_CLASS);
UserReq uq = (UserReq) parseXml(clazz,reader);
for (User u : uq.getUser())
{
System.out.println(u + "\n-----------");
}
}
private static Object parseXml(Class clazz, BeanReader reader)
throws IntrospectionException, IOException, SAXException
{
// 注册
reader.registerBeanClass(clazz);
// 解析xml
return reader.parse(new File(PATH));
}
}
------------------------User-------------------------
package com.huawei.t0901_betwixt;
public class User
{
// 姓名
private String name = "--";
// 电话
private String tel;
// 性别
private String sex;
// 地址
private String address;
/**
* 默认构造函数
*/
public User()
{
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getTel()
{
return tel;
}
public void setTel(String tel)
{
this.tel = tel;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
@Override
public String toString()
{
return "姓名:" + name + "\n性别:" + (sex.equalsIgnoreCase("M") ? "男" : "女")
+ "\n联系电话:" + tel + "\n家庭地址:" + address;
}
}
--------------------UserReq----------------------
package com.huawei.t0901_betwixt;
import java.util.ArrayList;
import java.util.List;
/**
* 用户请求
*
* @author chenglia
* @version [版本号, 2011-9-1]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class UserReq
{
// 用户
private List<User> user = new ArrayList<User>();
/**
* 默认构造函数
*/
public UserReq()
{
}
public List<User> getUser()
{
return user;
}
public void setUser(List<User> user)
{
this.user = user;
}
public void addUser(User u)
{
user.add(u);
}
}
--------------------userReq.bewixt-----------
<info primitiveTypes="element">
<element name="userReq">
<element name="user" property="user" updater="addUser" />
<addDefaults/>
</element>
</info>
----------------------user.bewixt-------------
<info primitiveTypes="element">
<element name="user">
<addDefaults/>
</element>
</info>