betwixt解析xml

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值