XML 根节点包含 PCData 时的 JAXB 注释
我正在尝试使用来自第三方网络服务的数据。不幸的是,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用@XmlValue
You can use @XmlValue