-- 作者:心宝
-- 发布时间:1/20/2012 12:54:00 PM
-- [求助]XML⇒JAVa 问题
从网上找了这样一段代码测试 代码: public class test { public static final void main(String[] args) throws Exception { BeanReader reader = new BeanReader(); reader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); reader.getBindingConfiguration().setMapIDs(false); // reader.registerBeanClass("personbean", PersonBean.class); reader.registerBeanClass(PersonBean.class); PersonBean person = (PersonBean)reader.parse(new File("C:\\person.xml")); System.out.println(person); } } Bean: public class PersonBean { private String name; private int age; /** Need to allow bean to be created via reflection */ public PersonBean() {} public PersonBean(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return "PersonBean[name=’" + name + "’,age=’" + age + "’]"; } } XML: <beans> <personbean> <age>22</age> <name>luna</name> </personbean> </beans> 结果一:跑这个时候,结果是null reader.registerBeanClass(PersonBean.class); 结果二:跑这个时候,结果是PersonBean[name=’luna’,age=’22’] reader.registerBeanClass("personbean", PersonBean.class); 问题,我如果想reader.registerBeanClass(PersonBean.class);用这种方式的话,要如果才能得到正确的结果呢。。。
|