XStream同时解析属性和值
我有以下 XML
<search ver="3.0">
<loc id="ARBA0009" type="1">Buenos Aires, Argentina</loc>
<loc id="BRXX1283" type="1">Buenos Aires, Brazil</loc>
<loc id="ARDF0127" type="1">Aeroparque Buenos Aires, Argentina</loc>
<loc id="MXJO0669" type="1">Concepcion De Buenos Aires, Mexico</loc>
<loc id="MXPA1785" type="1">San Nicolas De Buenos Aires, Mexico</loc>
<loc id="ARBA0005" type="1">Balcarce, Argentina</loc>
<loc id="ARBA0008" type="1">Bragado, Argentina</loc>
<loc id="ARBA0010" type="1">Campana, Argentina</loc>
<loc id="ARBA0016" type="1">Chascomus, Argentina</loc>
<loc id="ARBA0019" type="1">Chivilcoy, Argentina</loc>
</search>
和一个 City 类
public class City {
private String id;
private Integer type;
private String name;
// getters & setters...
}
我尝试使用以下别名来解析 XML
xStream.alias("search", List.class);
xStream.alias("loc", City.class);
xStream.useAttributeFor("id", String.class);
xStream.useAttributeFor("type", Integer.class);
但我不知道如何设置“loc”标记的值,如果我尝试转换 XML 中的 City 对象,我会得到
<search>
<loc id="ARBA0001" type="1">
<name>Buenos Aires</name>
</loc>
</search>
When我确实需要得到这个
<search>
<loc id="ARBA0001" type="1">Buenos Aires</loc>
</search>
然后,如果我尝试将 XML 解析为 City 对象,我会得到带有 null 值的字段“name”。
有人知道如何设置正确的别名来做到这一点吗?提前致谢。
I have the following XML
<search ver="3.0">
<loc id="ARBA0009" type="1">Buenos Aires, Argentina</loc>
<loc id="BRXX1283" type="1">Buenos Aires, Brazil</loc>
<loc id="ARDF0127" type="1">Aeroparque Buenos Aires, Argentina</loc>
<loc id="MXJO0669" type="1">Concepcion De Buenos Aires, Mexico</loc>
<loc id="MXPA1785" type="1">San Nicolas De Buenos Aires, Mexico</loc>
<loc id="ARBA0005" type="1">Balcarce, Argentina</loc>
<loc id="ARBA0008" type="1">Bragado, Argentina</loc>
<loc id="ARBA0010" type="1">Campana, Argentina</loc>
<loc id="ARBA0016" type="1">Chascomus, Argentina</loc>
<loc id="ARBA0019" type="1">Chivilcoy, Argentina</loc>
</search>
And a City class
public class City {
private String id;
private Integer type;
private String name;
// getters & setters...
}
I tried the following aliases to parse the XML
xStream.alias("search", List.class);
xStream.alias("loc", City.class);
xStream.useAttributeFor("id", String.class);
xStream.useAttributeFor("type", Integer.class);
But I can't figure out how to set the value of the "loc" tag, if I try to transform the City object in XML I get
<search>
<loc id="ARBA0001" type="1">
<name>Buenos Aires</name>
</loc>
</search>
When I really need to get this
<search>
<loc id="ARBA0001" type="1">Buenos Aires</loc>
</search>
Then, if I try to parse the XML to a City object I get the field "name" with a null value.
Anybody knows how to set te correct aliases to do this? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我终于找到了解决方案,转换器解决了这个问题,这里是代码
在设置别名的部分我还设置了 CityConverter
并且一切正常:)
I finally found the solution, a Converter solves this, here is the code
And in the part of setting the aliases I also set up the CityConverter
And all works fine :)
我发布此内容希望对其他人有所帮助,因为我花了很长时间才找到它......
http://fahdshariff.blogspot.com/2011 /12/using-xstream-to-map-single-element.html
答案是使用 @XStreamConverter - ToAttributedValueConverter
有很多有趣的转换器,它们提供各种有用的功能......
http://x-stream.github.io/converters.html
I am posting this in hopes that it might help others because it took me quite some time to find it...
http://fahdshariff.blogspot.com/2011/12/using-xstream-to-map-single-element.html
The answer is to use @XStreamConverter - ToAttributedValueConverter
There are lots of interesting converters that provide all sorts of useful functionalities...
http://x-stream.github.io/converters.html
XStream 看起来有点复杂,你可以在 JAXB 中执行以下操作:
XStream seems kind of complicated, you could do the following in JAXB: