, @XmlAttribute를 사용하고 name은 속성 이름입니다."/> , @XmlAttribute를 사용하고 name은 속성 이름입니다.">

 >  기사  >  Java  >  Java에서 구문 분석하여 xml을 Bean 인스턴스로 변환(순수 코드)

Java에서 구문 분석하여 xml을 Bean 인스턴스로 변환(순수 코드)

php是最好的语言
php是最好的语言원래의
2018-08-08 13:42:353353검색

최근 사용, 직접 작성한 데모 기록

  1. 루트 요소에 @XmlRootElement 주석을 사용합니다. name은 요소 이름입니다.

  2. 하위 요소 속성에는 @XmlElement를 사용하고, name은 요소 이름입니다.

  3. If 속성이 있습니다(예: 557fd2d237e2fd7075bc635fe517b85f). @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 &

위 내용은 Java에서 구문 분석하여 xml을 Bean 인스턴스로 변환(순수 코드)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.