从xml中初始化bean实例

需要从配置文件中初始化一个bean实例,并且里面需要包含一个Map类型的属性,相关代码如下:
xml
<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.6.0_23" class="java.beans.XMLDecoder"> 
 <object class="UserInfo"> 
  <void property="name"> 
   <string>Jimmy</string> 
  </void> 
  <void property="password"> 
   <string>123456</string> 
  </void> 
  <void property="test"> 
   <object class="java.util.HashMap"> 
    <void method="put"> 
     <string>test1</string> 
     <string>asfdasdf</string> 
    </void> 
    <void method="put"> 
     <string>test3</string> 
     <string>aaaaaaa</string> 
    </void> 
   </object> 
  </void> 
 </object> 
</java> 

java代码:
InputStream inputStream = new FileInputStream("c:/outxml.xml");
            XMLDecoder d = new XMLDecoder(inputStream, null, null);
            Object obj = d.readObject();
            if (obj instanceof UserInfo) {
                UserInfo u = (UserInfo) obj;
                Map m = u.getTest();
                System.out.println(m.get("test1"));
                System.out.println(m.get("test3"));
            }

UserInfo类:
public static class UserInfo implements Serializable {
        private String name;
        private String password;
        private Map<String, String> test;

        public Map<String, String> getTest() {
            return test;
        }

        public void setTest(Map<String, String> test) {
            this.test = test;
        }

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder();
            sb.append("UserInfo");
            sb.append("{name='").append(name).append('\'');
            sb.append(", password='").append(password).append('\'');
            sb.append(", test=").append(test);
            sb.append('}');
            return sb.toString();
        }
    }

你可能感兴趣的:(C++,c,bean,xml,C#)