XML 根节点包含 PCData 时的 JAXB 注释

发布于 2024-10-19 21:12:31 字数 1199 浏览 6 评论 0原文

我正在尝试使用来自第三方网络服务的数据。不幸的是,Web 服务正在以一种不寻常的 XML 格式返回数据。以下是我从服务接收的数据示例:

    <?xml version="1.0" encoding="utf-8"?>
    <string xmlns="http://mynamespace">(-76.4844131, 39.1031567, 0.0000000)
         (-76.4871168, 39.1031567, 0.0000000)
         (-76.4875889, 39.1042890, 0.0000000)
         (-76.4905500, 39.1052881, 0.0000000)
    </string>

根节点的内容不是包含航路点元素(纬度、经度、海拔)列表的根节点,而是一个大字符串值。虽然我可以解析该字符串,但我希望使用 JAXB(因为我使用 JERSEY 进行服务调用)将此返回的 XML 转换为 Java POCO。

理想情况下,最好将根节点的大字符串主体转换为具有路点列表的 POCO 类,但我想这需要在 JAXB 外部进行自定义 XML 反序列化。

但如果我可以使用 JAXB,我会对这样的事情感到高兴:

@XmlRootElement(name="string", namespace="http://mynamespace")
public class Route {
  private String _wayPoints;

  @ ???? 
  public String getWayPoints() { returns _waypoints; }
  public void setWayPoints(String wayPoints) { _wayPoints = wayPoints; }
}

不幸的是,我在 JAXB 中没有看到任何方法来告诉编组器我的属性之一应该是根元素或其他元素的内容。

理想情况下,如果我可以用类似以下内容注释我的属性,那就太好了:

@XmlElementContent(ElementName="string")

这会将我的 wayPoint 属性的内容编组/解组到我的根元素的内容(或内部文本)中。

有人遇到过类似的问题吗?有没有办法用 JAXB 做到这一点?或者我应该考虑解析字符串和/或编写自定义 XML 反序列化器。

谢谢!

I'm trying to consume data from a 3rd party web service. Unfortunately, the web service is returning data in what I'd say is an unusual XML format. Here is a sample of the data I am receiving from the service:

    <?xml version="1.0" encoding="utf-8"?>
    <string xmlns="http://mynamespace">(-76.4844131, 39.1031567, 0.0000000)
         (-76.4871168, 39.1031567, 0.0000000)
         (-76.4875889, 39.1042890, 0.0000000)
         (-76.4905500, 39.1052881, 0.0000000)
    </string>

Instead of having a root node that contains a list of waypoint elements (lat, lon, elevation), the root node's content is a large string value. While I could just parse the string, I was hoping to use JAXB (since I'm using JERSEY for my service calls) to convert this returned XML into a Java POCO.

Ideally, it would be nice to convert the large string body of the root node into a POCO class with a list of waypoints, but I imagine that would require custom XML deserialization outside of JAXB.

But I'd happy with something like this if I could use JAXB:

@XmlRootElement(name="string", namespace="http://mynamespace")
public class Route {
  private String _wayPoints;

  @ ???? 
  public String getWayPoints() { returns _waypoints; }
  public void setWayPoints(String wayPoints) { _wayPoints = wayPoints; }
}

Unfortunately, I haven't seen any way in JAXB to tell the marshaller that one of my properties should be the content of either the root element or some other element.

Ideally it would be nice if I could annotate my property with something like:

@XmlElementContent(ElementName="string")

and that would marshal/unmarshal the content of my wayPoint property into the content (or innertext) of my root element.

Has anybody had a similar issue? Is there a way to do this with JAXB? Or should I just consider parsing the string and/or writing a custom XML deserializer.

Thanks!

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

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

发布评论

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

评论(1

绮烟 2024-10-26 21:12:31

您可以使用@XmlValue

@XmlValue
public String getWayPoints() { returns _waypoints; }

You can use @XmlValue

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