XStream同时解析属性和值

发布于 2024-08-26 11:31:41 字数 1674 浏览 6 评论 0原文

我有以下 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 技术交流群。

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

发布评论

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

评论(4

土豪 2024-09-02 11:31:41

我终于找到了解决方案,转换器解决了这个问题,这里是代码

public class CityConverter implements Converter {

    public void marshal(Object value, HierarchicalStreamWriter writer, 
                                                               MarshallingContext context) {
        City city = (City) value;
        writer.addAttribute("id", city.getId());
        writer.addAttribute("type", city.getType().toString());
        writer.setValue(city.getName());
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        City city = new City();
        city.setName(reader.getValue());
        city.setId(reader.getAttribute("id"));
        city.setType(reader.getAttribute("type"));
        return city;
    }

    public boolean canConvert(Class clazz) {
        return clazz.equals(City.class);
    }

}

在设置别名的部分我还设置了 CityConverter

xStream.registerConverter(new CityConverter());
xStream.alias("search", List.class);
xStream.alias("loc", City.class);

并且一切正常:)

I finally found the solution, a Converter solves this, here is the code

public class CityConverter implements Converter {

    public void marshal(Object value, HierarchicalStreamWriter writer, 
                                                               MarshallingContext context) {
        City city = (City) value;
        writer.addAttribute("id", city.getId());
        writer.addAttribute("type", city.getType().toString());
        writer.setValue(city.getName());
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        City city = new City();
        city.setName(reader.getValue());
        city.setId(reader.getAttribute("id"));
        city.setType(reader.getAttribute("type"));
        return city;
    }

    public boolean canConvert(Class clazz) {
        return clazz.equals(City.class);
    }

}

And in the part of setting the aliases I also set up the CityConverter

xStream.registerConverter(new CityConverter());
xStream.alias("search", List.class);
xStream.alias("loc", City.class);

And all works fine :)

马蹄踏│碎落叶 2024-09-02 11:31:41

我发布此内容希望对其他人有所帮助,因为我花了很长时间才找到它......
http://fahdshariff.blogspot.com/2011 /12/using-xstream-to-map-single-element.html

答案是使用 @XStreamConverter - ToAttributedValueConverter

@XStreamAlias("error")
@XStreamConverter(value=ToAttributedValueConverter.class, strings={"message"})
public class Error {

  String message;

  ...

有很多有趣的转换器,它们提供各种有用的功能......
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

@XStreamAlias("error")
@XStreamConverter(value=ToAttributedValueConverter.class, strings={"message"})
public class Error {

  String message;

  ...

There are lots of interesting converters that provide all sorts of useful functionalities...
http://x-stream.github.io/converters.html

疯了 2024-09-02 11:31:41

XStream 看起来有点复杂,你可以在 JAXB 中执行以下操作:

public class City { 

    @XmlAttribute private String  id; 
    @XmlAttribute private Integer type; 
    @XmlValue private String  name; 

    // getters & setters... 
}

XStream seems kind of complicated, you could do the following in JAXB:

public class City { 

    @XmlAttribute private String  id; 
    @XmlAttribute private Integer type; 
    @XmlValue private String  name; 

    // getters & setters... 
}
浮生未歇 2024-09-02 11:31:41
@XStreamConverter(value= ToAttributedValueConverter.class, strings={"name"})
public class City { 

    @XStreamAsAttribute @XStreamAlias("id") private String  id; 
    @XStreamAsAttribute @XStreamAlias("type") private Integer type; 
    private String  name; 

    // getters & setters... 
}
@XStreamConverter(value= ToAttributedValueConverter.class, strings={"name"})
public class City { 

    @XStreamAsAttribute @XStreamAlias("id") private String  id; 
    @XStreamAsAttribute @XStreamAlias("type") private Integer type; 
    private String  name; 

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