以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 WORD to XML, HTML to XML 』  (http://bbs.xml.org.cn/list.asp?boardid=13)
----  [求助]XML⇒JAVa 问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=13&rootid=&id=123235)


--  作者:心宝
--  发布时间: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);用这种方式的话,要如果才能得到正确的结果呢。。。


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms