我有一个 XMLGregorianCalendar 对象,我正在尝试将其封送到 xml 字符串中。我通过解组另一个 xml 对象收到了这个对象。两者都是“dateTime”类型,所以它们应该完全相同......
然而,当我对其进行封送时,它在 xml 中显示为空白。
为了说明这个问题,我将所有内容都剥离到了最基本的部分,并在此处的示例中使其通用。2个java文件,复制,粘贴,按原样运行。您应该收到的输出是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TheObject>
<DOB>2016-09-16</DOB>
</TheObject>
Run Code Online (Sandbox Code Playgroud)
但是,唉,它返回:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TheObject>
<DOB></DOB>
</TheObject>
Run Code Online (Sandbox Code Playgroud)
注意:在 Pastebin 示例中,我动态创建一个 xmlGregorianCalendar,而不是像下面的代码那样从另一个对象中获取一个 xmlGregorianCalendar,因此从技术上讲,这不是同一件事,但我认为最终它说明了完全相同的问题...请纠正我我错了...
要为我的具体问题添加更多背景信息:
//Here are the objects themselves (names changed to protect the innocent)
//complete with annotations...
public class Object1{
...
@XmlElement(name = "DOB")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar dob;
...
}
public class Object2{
...
@XmlElement(name = "DOB")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar dob;
...
}
//and here's the snippet where the date object(date of birth) gets set
//from one object to another.
object2.setDOB(object1.getDOB());
//and finally, marshalling it to an xml string
private String marshallTheObject(Object2 theObject) throws JAXBException{
JAXBContext jaxbContext = JAXBContext.newInstance(Object2.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(object2, sw);
String output = sw.toString();
return output;
}
//the xml output shows: <DOB></DOB> instead of the date
Run Code Online (Sandbox Code Playgroud)
我正在使用 java 8 捆绑的 jaxb 版本...
所以我的问题是:这是某种错误吗?如果不是,我做错了什么?如何在不修改生成的 java 代码的情况下解决这个问题?我也无法编辑用于生成它的 xsd 文件...
编辑:作为参考,xsd 文件将 DOB 列出为:
<xs:element name="DOB" type="xs:dateTime" />
Run Code Online (Sandbox Code Playgroud)
虽然我不被允许直接修改 xsd 文件,但我没有意识到我可以更改通过绑定文件生成类本身的方式。在这种情况下我没有使用 Maven,所以确定解决方案有点困难。我通过使用 eclipse 内置的功能生成类,该功能从 xsd 文件生成 jaxb 类。
我在这里了解了有关绑定文件的更多信息
我不太确定该文件需要放置在 Maven 的位置,但是以 Eclipse 方式执行此操作并不重要 - 您可以在 jaxb 类生成向导期间指定绑定文件的位置。
生成后,您需要编写自己的 xml 适配器代码。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class CalendarDateTimeAdapter extends XmlAdapter<String, Date> {
//Sadly my specific situation requires me to strip the time off of all dateTime objects
//It's bad, but I didn't get to design the system, so this is the best compromise...
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Override public Date unmarshal(String value) throws ParseException {
synchronized (sdf){
return sdf.parse(value);
}
}
@Override public String marshal(Date value) {
if(value == null) { return null; }
synchronized(sdf){
return sdf.format(value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
确保您的类与您在绑定文件中指定的类匹配...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jaxb:bindings>
<jaxb:bindings version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<!-- Prevent wrapping data types into JAXBElements. -->
<jaxb:globalBindings generateElementProperty="false">
<!-- Use java.util.Date instead of XMLGregorianCalendar. -->
<xjc:javaType name="java.util.Date" xmlType="xs:dateTime"
adapter="com.package.location.adapter.CalendarDateTimeAdapter"/>
<xjc:javaType name="java.util.Date" xmlType="xs:date"
adapter="com.package.location.adapter.CalendarDateAdapter"/>
<xjc:javaType name="java.util.Date" xmlType="xs:time"
adapter="com.package.location.adapter.CalendarTimeAdapter"/>
</jaxb:globalBindings>
</jaxb:bindings>
Run Code Online (Sandbox Code Playgroud)
然后,在进行编组时,使用 setAdapter 函数来使用它。
private String marshallObject(MyObject myObject) throws JAXBException{
JAXBContext jaxbContext = JAXBContext.newInstance(MyObject.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setAdapter(new CalendarDateTimeAdapter());
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(myObject, sw);
String output = sw.toString();
return output;
}
Run Code Online (Sandbox Code Playgroud)
现在它们将解析为 Date 对象而不是 XMLGregorianCalendar 对象。显然,Date 对象比 XMLGregorianCalendars 工作得更好......
我仍然感到不安的是,输入的未编组的 xml 不会编组为输出的相同 xml,但无论如何,这正是我为使其全部开始工作而所做的。我确信我在这里做了一些违反惯例的事情,如果有,请告诉我。
我再次提醒读者,我没有使用 Maven,也没有使用任何类型的框架(例如 SpringMVC)。