问题
In ActionScript 3, how to decode from xml to ActionScript class ?
I could encode from ActionScript class to xml by using XmlEncoder.
The xml schema I used at that time is this.
[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.
But I could not decode from xml to ActionScript class by using this schema and XmlDecoder. (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.
However, I need a schema file for client side of ActionScript to marshal and unmarshall.
Please tell me any solutions or examples...
回答1:
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());
}
}
来源:https://stackoverflow.com/questions/4621962/in-actionscript-3-how-to-decode-from-xml-to-actionscript-class