XML in Java converted to bean instance parsing (pure code)

Source: Internet
Author: User
Recently used to record a self-written demo

    1. Using the @xmlrootelement annotation on the root element, name is the element name

    2. child element attributes use @xmlelement,name as the element name

    3. If there are attributes, such as <emplyee hobby= "Swim", then use @xmlattribute,name as the property 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; }}

Parsing classes

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);        }            }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.