首頁 > Java > java教程 > java 中xml轉換為Bean實例解析(純程式碼)

java 中xml轉換為Bean實例解析(純程式碼)

php是最好的语言
發布: 2018-08-08 13:42:35
原創
3432 人瀏覽過

最近用到,記錄一個自己寫的demo

  1. 在根元素上使用@XmlRootElement註解,name為元素名稱

  2. 子元素屬性使用@XmlElement,name為元素名稱

  3. 若有屬性,例如,則使用@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);
        }
        
    }
登入後複製

相關建議:

Java&Xml教學(八)使用JDOM將Java物件轉換為XML

#Jaxb2實作Bean與xml互轉的範例程式碼詳解

以上是java 中xml轉換為Bean實例解析(純程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板