如何通过一个XML转换日期(动作3)java.util.Date? [英] How to convert Date(ActionScript 3) to java.util.Date through a xml?
问题描述
我想通过一个XML转换日期(动作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的使用具有其模式文件XMLEn codeR。
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);
}
}
然后在您的用户类指定此XmlAdapter的userDate属性:
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秘密 - weapon.html
- http://bdoughan.blogspot.com/2010/ 12 / JAXB和 - 不可改变objects.html
- 的http://bdoughan.blogspot.com/2010/12/re$p$psent-string-values-as-element.html
- 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
更新
根据您的意见,如果要指定这是一个包级别注释,你需要包括一类称为的包信息的在同一个包为您的模型类。这个类看起来像:
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重新presentation延伸,我是技术主管:
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:日期时间):
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转换日期(动作3)java.util.Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!