java 中xml轉換為Bean執行個體解析(純程式碼)

來源:互聯網
上載者:User
最近用到,記錄一個自己寫的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;    }}

解析類

 public static void main(String[] args) throws JAXBException {        JAXBContext context = JAXBContext.newInstance(Employees.class);        Unmarshaller createUnmarshaller = context.createUnmarshaller();        Object unmarshal = createUnmarshaller.unmarshal(                new File("D:/java/workspacedev/JavaTest/xml/employees.xml"));        Employees em = (Employees) unmarshal;        List<Employee> list = em.geteList();        for (Employee employee : list) {            System.out.println(employee);        }            }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.