java 中xml转换为Bean

最近用到,记录一个自己写的demo

 

  1. 在根元素上使用@XmlRootElement注解,name为元素名
  2. 子元素属性使用@XmlElement,name为元素名
  3. 若有属性,例如<emplyee hobby="swim" >,则使用@XmlAttribute,name为属性名

 

 

xml:

<?xml version="1.0" encoding="UTF-8"?>
<employees>
    <employee>
        <userId>johnsmith@company.com</userId>
        <password>abc123_</password>
        <name>John Smith</name>
        <age>24</age>
        <gender>Male</gender>
    </employee>
    <employee>
        <userId>christinechen@company.com</userId>
        <password>123456</password>
        <name>Christine Chen</name>
        <age>27</age>
        <gender>Female</gender>
    </employee>
</employees>

 

Employees:

import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "employees")
public class Employees {

    private List<Employee> eList;
    @XmlElement(name = "employee")
    public List<Employee> geteList() {
        return eList;
    }

    public void seteList(List<Employee> eList) {
        this.eList = eList;
    }

}

Employee:

import javax.xml.bind.annotation.XmlElement;

public class Employee {
    private String userId;
    private String password;
    private String name;
    private String age;
    private String gender;
    @Override
    public String toString() {
        return "Employee [userId=" + userId + ", password=" + password
                + ", name=" + name + ", age=" + age + ", gender=" + gender
                + "]";
    }
    @XmlElement(name="userId")
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    @XmlElement(name="password")
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @XmlElement(name="name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @XmlElement(name="age")
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @XmlElement(name="gender")
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }

}

 

解析类

 

@Component
@XmlRootElement(name = "employees")
public class XmlMapping {
    private List<Employee> eList;

    @XmlElement(name = "employee")
    public List<Employee> getList() {
        return eList;
    }

    public void setList(List<Employee> eList) {
        this.eList = eList;
    }

    /**
     * 文件映射路径
     */
    private String resoursePath = Objects.requireNonNull(XmlMapping.class.getClassLoader().getResource("empolyee.xml")).getFile();

    @PostConstruct
    public void init() {
        try {
            System.out.println(resoursePath);
            JAXBContext context = JAXBContext.newInstance(XmlMapping.class);
            Unmarshaller createUnmarshaller = context.createUnmarshaller();
            Object unmarshal = createUnmarshaller.unmarshal(
                    new File(resoursePath));
            XmlMapping em = (XmlMapping) unmarshal;
            List<Employee> list = em.getList();
            System.out.println(list);
            eList=list;
        } catch (Exception e) {
            //LOG
        }
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值