- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 XMLGregorianCalendar 对象,我正试图将其编码为 xml 字符串。我通过解码另一个 xml 对象收到了这个对象。两者都是“dateTime”类型,所以它们应该完全相同......
然而,当我编码它时,它在 xml 中显示为空白。
为了说明这个问题,我将所有内容剥离到最基本的部分并使其通用化 in this example here . 2个java文件,复制,粘贴,按原样运行。您应该收到的输出是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TheObject>
<DOB>2016-09-16</DOB>
</TheObject>
但是,唉,它返回:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TheObject>
<DOB></DOB>
</TheObject>
注意:在 pastebin 示例中,我动态创建了一个 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
我正在使用 java 8 捆绑的 jaxb 版本...
所以我的问题是:这是某种错误吗?如果不是,我做错了什么?如何在不修改生成的 Java 代码的情况下解决这个问题?我也无法编辑用于生成它的 xsd 文件...
编辑:作为引用,xsd 文件将 DOB 列为:
<xs:element name="DOB" type="xs:dateTime" />
最佳答案
虽然不允许我直接修改 xsd 文件,但我没有意识到我可以通过绑定(bind)文件更改类本身的生成方式。在这种情况下我没有使用 maven,所以确定解决方案有点困难。我通过使用 eclipse 中内置的功能生成类,该功能从 xsd 文件生成 jaxb 类。
我了解了更多关于绑定(bind)文件的信息 here
我不太确定该文件需要放置在 Maven 的什么位置,但是以 eclipse 的方式执行它并不重要 - 您可以在 jaxb 类生成向导期间指定绑定(bind)文件的位置。
生成后,您需要编写自己的 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);
}
}
}
确保您的类与您在绑定(bind)文件中指定的相匹配...
<?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>
然后,在进行编码时,使用 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;
}
现在它们将解析为 Date 对象而不是 XMLGregorianCalendar 对象。显然,Date 对象比 XMLGregorianCalendars 工作得更好......
我仍然感到不安的是,进入的未编码的 xml 没有编码成为相同的 xml 出去,但无论如何,这正是我所做的,使它全部开始工作。我确定我在这里做了一些违反惯例的事情,如果我做了,请告诉我。
再次提醒读者,我没有使用 maven,也没有使用任何类型的框架(例如 SpringMVC)。
关于java - 未指定时间时,JAXB 将 dateTime 编码为空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39538056/
我正在考虑使用 JAXB 进行 XML 解析,但到目前为止我遇到了一些问题,这让我相信它可能不够灵活,无法满足我的需求。 我将解析第三方提供的 XML 以符合我将发布的 XSD。所以我想足够灵活地处理
我正在考虑使用 JAXB 进行 XML 解析,但到目前为止我遇到了一些问题,这让我相信它可能不够灵活,无法满足我的需求。 我将解析第三方提供的 XML 以符合我将发布的 XSD。所以我想足够灵活地处理
我有一个 xsd,用于使用 JAXB 在 Java 中生成对象模型,并且我希望将它生成的列表重命名为 xyzList 而不是 xyz。有没有办法做到这一点,而不必在绑定(bind)文件中为每个列表添加
我们正在使用 JAXB 构建许多开发人员应用程序,并不断遇到问题,这些问题都归结为 JAXB 对象的生产者和消费者之间的“版本”不匹配。 过程并没有减轻痛苦,所以我正在考虑一些类似于 JAXB 的 C
我们有一个在一个属性上带有 JAXB 注释的类。然后我们有几个子类来注释其余的重要数据。然而,我们有一个子类,我们想忽略父类注释,这样它就不会被编码。这是一些示例代码。 父类: @XmlType(na
我一直在使用 Oracle JDK 1.7 附带的默认 Sun JAXB 实现。 不幸的是,我有一些非常复杂的 XSD 模式可以使用,而且我发现了 XSD 到 Java 引擎中的一个错误(在 this
我有这个 Maven“任务”来使用 JAXB 从 XSD 文件生成 Java 类。 com.sun.tools.xjc.maven2
我想要这样的 XML: Foo 我可以通过一个看起来像这样的 JAXB 类成功地做到这一点: @XmlRootElement(name="simple") class Simple { @Xm
我有一个 POJO,我想将其编码并作为 xml 发送,但也想解编从同一个 Web 服务返回的数据。但是,此 POJO 中有某些字段,我不想发送,但可能在服务器返回的 xml 中。 这里的问题是,如果我
嘿伙计们,希望这里是一个简单的好东西。 我正在使用 JAXB 从架构生成类,并且我希望能够使用访问者模式来处理它们。 为此,我认为我需要每个 JAXB 生成的类来实现我定义的接口(interface)
我有一个 POJO,我想将其编码并作为 xml 发送,但也想解编从同一个 Web 服务返回的数据。但是,此 POJO 中有某些字段,我不想发送,但可能在服务器返回的 xml 中。 这里的问题是,如果我
这是我的转换代码。当我们处理大数据时,这需要很长时间......调用该方法几乎一百万次......我们可以清楚地看到它持有线程一段时间。 请建议我一些提高性能的方法! public class Gen
我有一个可以成功编码的 jaxb 对象,它有一个列表对象,然后我创建一个如下所示的新对象 public class Sub extends SuperJAXBClass{ @Override publ
我已经将jaxb库更新为2.0版。 我正在使用以下jars jaxb-api-2.0.jar和jaxb-imp-2.0.jar。 现在的问题是,它没有使用@XmlRootElement(name =“
我在使用 JAXB 时面临以下问题:看起来 JAXB 正在分析从最深的子类到父类的属性,并且子属性具有优先级。我想以某种方式改变这种行为。特别是: 子类: package test.sub; impo
我有以下 XML 消息,我需要反序列化它 我该如何处理? 最佳答案 @XmlElements注释用于映射选择结构。 http://b
我们想用 JAX-RS 和 JAXB 实现一个 RESTful-Web 服务。我们有一个使用 xml 的 PUT 方法,如下所示: -1 test test@gmx.de
我有以下代码 @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class SamplePojo { private S
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 2 年前。
所以我有一个 Jersey 的 REST 服务设置。 我的 POJO 被简单地注释为@XmlRootElement 我向我的 REST 服务发送了一个 POST 请求,一切正常。 1- 我向服务发送了
我是一名优秀的程序员,十分优秀!