1

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

我可以使用 XmlEncoder 从 ActionScript 类编码为 xml。

我当时使用的xml架构是这样的。

[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>

这个模式是由 Ant(schemagen) 任务使用 POJO(User.java) 创建的,没有注释。

但我无法使用此架构和 XmlDecoder 从 xml 解码为 ActionScript 类。(正确地,我无法从 Object 类型转换为 User 类型。)

我不想在 Java 类中放置任何注释,如 @XmlRootElement 或 @XmlType。

但是,我需要一个用于 ActionScript 客户端的架构文件来编组和解组。

请告诉我任何解决方案或示例...

4

1 回答 1

0

以下类:

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

}

可用于解组以下 XML:

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

使用以下代码,无需对 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());
    }
}
于 2011-01-07T14:23:15.427 回答