将 XML 数据直接解组到对象中

发布于 2024-09-18 23:09:18 字数 1199 浏览 8 评论 0原文

我正在尝试使用 SimpleXML 从 Java 中的 XML 对象中解组数据。整个事情的重点是将 API 从 JAXB 转换为 SimpleXML。因此,我使用了注释的方式来解析文档。下面是一些代码:

在 User 类中:

@Element(name="created", required=false)
private Date created;

编写 API 的程序员使用 DateAdapter 将从 XML 中拉入的字符串直接转换为日期。我尝试将其转换为 SimpleXML。我的假设是 Transformer 使用了相同的方法。这是之前和之后的代码...

之前:

public class DateAdapter extends XmlAdapter<String, Date> {

  DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  public Date unmarshal(String date) throws Exception {
    return df.parse(date);
  }

  public String marshal(Date date) throws Exception {
    return df.format(date);
  }
}

之后:

public class DateAdapter implements Transform<Date> {
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  @Override
  public Date read(String date) throws Exception {
    return df.parse(date);
  }

  @Override
  public String write(Date date) throws Exception {
    return df.format(date);
  }
}

我假设我进行了错误的转换,因为我现在遇到了无法解析的日期错误。奇怪的是,即使我将 if-else 或 try-catch 块放入读取和写入方法中,我仍然会收到错误。

所以我认为主要问题是,如何正确编写一个像 JAXB 这样的适配器来在 XML 中的字符串和 Date 对象之间编组/解组。

I am trying to unmarshall data from an XML object in Java using SimpleXML. The point of the whole thing was to convert an API from JAXB to SimpleXML. Therefore, I used the annotation way of parsing the document. Here is some code:

In the User class:

@Element(name="created", required=false)
private Date created;

The programmer who wrote the API used a DateAdapter to turn the String pulled in from the XML directly into a date. I attempted to convert it to SimpleXML. My assumption is that a Transformer used the same approach. Here is that code before and after...

Before:

public class DateAdapter extends XmlAdapter<String, Date> {

  DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  public Date unmarshal(String date) throws Exception {
    return df.parse(date);
  }

  public String marshal(Date date) throws Exception {
    return df.format(date);
  }
}

After:

public class DateAdapter implements Transform<Date> {
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  @Override
  public Date read(String date) throws Exception {
    return df.parse(date);
  }

  @Override
  public String write(Date date) throws Exception {
    return df.format(date);
  }
}

I assume I did the conversion wrong because I am now getting Unparsable date errors. The weird part is that, even if I put if-else or try-catch blocks within the read and write methods I still get the error.

So I think the main question is, how do I properly write an adapter like the JAXB one to marshall/unmarshall between the String from the XML and a Date object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×眷恋的温暖 2024-09-25 23:09:18

我解决了这个问题。它与我正在处理的 API 本身无关,而是由实际 XML 框架解组时将双精度数转换为日期。 XML 中存储的双精度是以秒为单位的 Epoch 时间,小数点表示毫秒。 SimpleXML 似乎无法正确处理小数。我的解决方法是只要求一个双精度值,而不是将数据解组到日期中。所以我摆脱了整个适配器,只是改为

@Element(name="created", required=false)
private Date created;

@Element(name="created", required=false)
private double created;

确信有一种简单的方法可以在事后将其转换为日期,但我认为最好保持原样。

I fixed the problem. It had nothing to do with the API I was working on itself, but rather the conversion of a double to a date when unmarshalled by the actually XML framework. The double stored in the XML was in Epoch time in seconds, with decimals for milliseconds. SimpleXML could not seem to handle the decimals properly. My workaround was to just ask for a double instead of having the data be unmarshalled into a Date. So I got rid of the whole adapter and just changed

@Element(name="created", required=false)
private Date created;

to

@Element(name="created", required=false)
private double created;

I'm sure that there is a way with Simple to convert this into a Date after the fact, but I figure it's better to just leave it as is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文