如何通过 xml 将 Date(ActionScript 3) 转换为 java.util.Date? [英] How to convert Date(ActionScript 3) to java.util.Date through a xml?
问题描述
我想通过 xml 将 Date(ActionScript 3) 转换为 java.util.Date.
I want to convert Date(ActionScript 3) to java.util.Date through a xml.
首先,像这样编写一个用户定义的 ActionScript 类.
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;
}
其次,创建它的实例并设置每个值,因此它将 ActionScript 类转换为 xml,以便使用具有其架构文件的 XMLEncoder.
Second, create its instance and set each of values, so it converts ActionScript class to a xml for using XMLEncoder having its schema file.
这是结果xml,并将此xml发送到服务器以使用HTTPService.
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>
最后,在 Java 的服务器端,我想将此 xml 转换为这样的 Java 类,以便使用 JAXB Unmarshaller.
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;
}
}
但是,结果,UserDate"属性只会为空...
But, as a result, "UserDate" property is only going to be null...
为什么UserDate"属性为空?如果有,请告诉我解决方案.
Why "UserDate" property is null ? And, please tell me solutions if any.
推荐答案
您可以使用 XmlAdapter 来实现:
You can use an XmlAdapter to accomplish this:
package example;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class DateAdapter extends XmlAdapter<String, Date> {
private SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss zZ yyyy");
@Override
public Date unmarshal(String v) throws Exception {
System.out.println(format.parse(v));
return format.parse(v);
}
@Override
public String marshal(Date v) throws Exception {
return format.format(v);
}
}
然后在 User 类的 userDate 属性上指定此 XmlAdapter:
Then specify this XmlAdapter on the userDate property on your User class:
package example;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement(name="User")
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;
}
@XmlJavaTypeAdapter(DateAdapter.class)
public Date getUserDate() {
return userDate;
}
public void setUserDate(Date userDate) {
this.userDate = userDate;
}
}
有关 XmlAdapter 的更多信息,请参阅:
For more information on XmlAdapter see:
- http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html
- http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html
- http://bdoughan.blogspot.com/2010/12/represent-string-values-as-element.html
更新
根据您的评论,如果您想将其指定为包级注释,您需要在与模型类相同的包中包含一个名为 package-info 的类.这个类看起来像:
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:
@XmlJavaTypeAdapter(value=DateAdapter.class, type=Date.class)
package example;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
如果您想要另一种方法来指定 JAXB 元数据,您可以使用 EclipseLink JAXB (MOXy) 中的 XML 表示扩展,我是技术负责人:
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:
或者,您可以确保传递给 JAXB 的日期采用以下格式 (xsd:dateTime):
Alternatively, you could ensure that the date that is passed to JAXB is in the following format (xsd:dateTime):
[-]CCYY-MM-DDThh:mm:ss[Z|(+|-)hh:mm]
这篇关于如何通过 xml 将 Date(ActionScript 3) 转换为 java.util.Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!