简体   繁体   English

在ActionScript 3中,如何从xml解码为ActionScript类?

[英]In ActionScript 3, how to decode from xml to ActionScript class?

In ActionScript 3, how to decode from xml to ActionScript class ? 在ActionScript 3中,如何从xml解码为ActionScript类?

I could encode from ActionScript class to xml by using XmlEncoder. 我可以使用XmlEncoder从ActionScript类编码为xml。

The xml schema I used at that time is this. 我当时使用的xml模式就是这个。

[schema1.xsd] [schema1.xsd]

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="user">
    <xs:sequence>
      <xs:element name="id" type="xs:string" minOccurs="0"/>
      <xs:element name="password" type="xs:string" minOccurs="0"/>
      <xs:element name="userDate" type="xs:dateTime" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

This schema is created by Ant(schemagen) task using POJO(User.java) with no annotations. 此架构是由Ant(schemagen)任务使用POJO(User.java)创建的,没有注释。

But I could not decode from xml to ActionScript class by using this schema and XmlDecoder. 但是我无法使用此架构和XmlDecoder从xml解码为ActionScript类。 (In correct, I can't be done casting from Object type to User type.) (正确的说,我无法完成从对象类型到用户类型的转换。)

I want not to put any annotations like @XmlRootElement or @XmlType in Java class. 我不想在Java类中放置任何@XmlRootElement或@XmlType之类的注释。

However, I need a schema file for client side of ActionScript to marshal and unmarshall. 但是,我需要一个模式文件供ActionScript的客户端编组和解组。

Please tell me any solutions or examples... 请告诉我任何解决方案或示例...

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:

<?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: 使用以下代码,而无需在User类上添加任何注释:

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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM