• XStream xml转java对象


    1:引入jar qn 

    <dependency>
                <groupId>xstream</groupId>
                <artifactId>xstream</artifactId>
                <version>1.1.3</version>
            </dependency>
            <dependency>
                <groupId>xpp3</groupId>
                <artifactId>xpp3_min</artifactId>
                <version>1.1.3.4.O</version>
            </dependency>

    2:XML (admin.xml)

    <ConfigUser>
        <type>ADMIN</type>
        <users>
            <AdminUser>
                <name>zhaoyb</name>
                <pwd>abc123</pwd>
                <ip>
                    <string>127.0.0.1</string>
                    <string>203.119.80.128</string>
                </ip>
            </AdminUser>
            <AdminUser>
                <name>liangqunxing</name>
                <pwd>abc123</pwd>
                <ip>
                    <string>127.0.0.1</string>
                    <string>202.173.100.126</string>
                </ip>
            </AdminUser>
            <AdminUser>
                <name>liuyu</name>
                <pwd>abc123</pwd>
                <ip>
                    <string>127.0.0.1</string>
                    <string>203.119.80.108</string>
                </ip>
            </AdminUser>
        </users>
    </ConfigUser>

    3:java 代码

    InputStream xmlInputStream = new ClassPathResource("admin.xml").getInputStream();
                XStream xStream = new XStream();
                xStream.alias("AdminUser", AdminUser.class);
                xStream.alias("ConfigUser", ConfigUsers.class);
                ConfigUsers users = (ConfigUsers) xStream.fromXML(xmlInputStream);
                List<AdminUser> adminUsers = users.getUsers();
    public class ConfigUsers {
         private String type;
            private List<AdminUser> users;
    
            public String getType() {
                return type;
            }
    
            public void setType(String type) {
                this.type = type;
            }
    
            public List<AdminUser> getUsers() {
                return users;
            }
    
            public void setUsers(List<AdminUser> users) {
                this.users = users;
            }
    
            @Override
            public String toString() {
                return new ToStringBuilder(this)
                        .append("type", type)
                        .append("users", users)
                        .toString();
            }
    }
    public class AdminUser {
        private String name;
        private String pwd;
        private List<String> ip;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<String> getIp() {
            return ip;
        }
    
        public void setIp(List<String> ip) {
            this.ip = ip;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    
        @Override
        public String toString() {
            return new ToStringBuilder(this).append("name", name)
                    .append("pwd", pwd).append("ip", ip).toString();
        }
    }
  • 相关阅读:
    DOM节点类型
    javascript中的变量、作用域
    this 不同情况指代的对象
    BOM对象节点
    浏览器兼容性
    总结
    javascript事件流讲解和实例应用
    7.20
    7.16总结
    飞机大战
  • 原文地址:https://www.cnblogs.com/feiyun126/p/5012829.html
Copyright © 2020-2023  润新知