In ActionScript 3, how to decode from xml to ActionScript class?

若如初见. 提交于 2019-12-04 19:06:20

The following class:

import java.util.Date;

public class User {

    private String id;
    private String password;
    private Date userDate;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getUserDate() {
        return userDate;
    }

    public void setUserDate(Date userDate) {
        this.userDate = userDate;
    }

}

Can be used to unmarshal the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <id>123</id>
   <password>foo</password>
   <userDate>2011-01-07T09:15:00</userDate>
</root>

Using the following code without requiring any annotations on the User class:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(User.class);

        StreamSource source = new StreamSource("input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement<User> root = unmarshaller.unmarshal(source, User.class);

        User user = root.getValue();
        System.out.println(user.getId());
        System.out.println(user.getPassword());
        System.out.println(user.getUserDate());
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!