如何通过xml将Date(ActionScript 3)转换为java.util.Date?
我想通过 xml 将 Date(ActionScript 3) 转换为 java.util.Date。
首先,像这样编写一个用户定义的 ActionScript 类。
public class User
{
public function User()
{
userDate = new Date();
}
public var id:String = null;
public var password:String = null;
public var userDate:Date = null;
}
其次,创建其实例并设置每个值,以便将 ActionScript 类转换为 xml,以便使用具有其架构文件的 XMLEncoder。
这是结果 xml,并将此 xml 发送到服务器以使用 HTTPService。
<User>
<id>system</id>
<password>manager</password>
<userDate>Fri Jan 14 09:02:17 GMT+0900 2011</userDate>
</User>
最后,在 Java 的服务器端,我想将此 xml 转换为 Java 类,如下所示,以便使用 JAXB Unmarshaller。
public class User {
public User() {
}
private String id;
private String password;
private Date userDate;
public void setId(String id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserDate(Date userDate) {
this.userDate = userDate;
}
public String getId() {
return id;
}
public String getPassword() {
return password;
}
public Date getUserDate() {
return userDate;
}
}
但是,结果,“UserDate”属性只会为 null...
为什么“UserDate”属性为 null ? 并且,请告诉我解决方案(如果有)。
I want to convert Date(ActionScript 3) to java.util.Date through a xml.
First, write a user defined ActionScript class like this.
public class User
{
public function User()
{
userDate = new Date();
}
public var id:String = null;
public var password:String = null;
public var userDate:Date = null;
}
Second, create its instance and set each of values, so it converts ActionScript class to a xml for using XMLEncoder having its schema file.
This is the result xml, and send this xml to a server for using a HTTPService.
<User>
<id>system</id>
<password>manager</password>
<userDate>Fri Jan 14 09:02:17 GMT+0900 2011</userDate>
</User>
Finally, In server side of Java, I want to convert this xml to Java class like this for using JAXB Unmarshaller.
public class User {
public User() {
}
private String id;
private String password;
private Date userDate;
public void setId(String id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserDate(Date userDate) {
this.userDate = userDate;
}
public String getId() {
return id;
}
public String getPassword() {
return password;
}
public Date getUserDate() {
return userDate;
}
}
But, as a result, "UserDate" property is only going to be null...
Why "UserDate" property is null ?
And, please tell me solutions if any.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Flash
Date
和java.util.Date
的文本表示形式可能不兼容。由于这两个日期对象在内部都是基于自 1970 年 1 月 1 日以来的毫秒数,因此我建议在 AS3 中使用date.time
来获取日期的整数值,将其发送到 Java,然后使用date.setTime()
设置正确的日期。The text representation of your Flash
Date
andjava.util.Date
are probably not compatible. Since both date objects are internally based on the number of milliseconds since January 1, 1970, I would recommend usingdate.time
in AS3 to get the integer value of the date, sending it to Java, and then usingdate.setTime()
to set the correct date.您可以使用 XmlAdapter 来完成此操作:
然后在 User 类的 userDate 属性上指定此 XmlAdapter:
有关 XmlAdapter 的详细信息,请参阅:
更新
根据您的评论,如果您想将其指定为包级注释,您需要包含一个名为package-info 与模型类位于同一包中。这个类看起来像:
如果您想要一种指定 JAXB 元数据的替代方法,您可以使用 EclipseLink JAXB (MOXy) 中的 XML 表示扩展,我是技术主管:
或者,您可以确保传递给 JAXB 的日期采用以下格式 (xsd:dateTime):
You can use an XmlAdapter to accomplish this:
Then specify this XmlAdapter on the userDate property on your User class:
For more information on XmlAdapter see:
UPDATE
Based on your comments, if you want to specify this as a package level annotation you need to include a class called package-info in the same package as your model classes. This class will look like:
If you want an alternative means to specify JAXB metadata you could use the XML representation extension in EclipseLink JAXB (MOXy), I'm the tech lead:
Alternatively, you could ensure that the date that is passed to JAXB is in the following format (xsd:dateTime):
您可能只想研究使用 BlazeDS 来处理 AMF(序列化 AS3)和序列化 POJO 之间的转换工作。
我没有太多使用 JAXB 进行解组的经验,但在互联网上发现了这一点。
XML 架构日期时间类型的正确格式是:
yyyy-mm-ddThh:mm:ss
如果将日期更改为以下内容,您还会看到错误吗?
2006-02-01T08:00:00
如果你在 Flex 中,你可以使用 DateFormatter,否则你可以在 AS3 端创建一个 getter 来返回适当格式的日期。 weltraumpirat 的回答似乎同样有效。
You may just want to look into using BlazeDS to handle the job of conversion between AMF (serialized AS3) and serialized POJO.
I don't have a lot of experience with using JAXB to unmarshall but found this out on the interwebs.
The correct format for an XML Schema dateTime type is:
yyyy-mm-ddThh:mm:ss
If you change your date to the following do you still see the error?
2006-02-01T08:00:00
You could use a DateFormatter if your in Flex otherwise you could make a getter on the AS3 side to return the appropriately formatted date. weltraumpirat's answer seems equally valid.