XStream 同时解析属性和值 [英] XStream parse attributes and values at the same time

查看:40
本文介绍了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>

还有一个城市类

public class City {

    private String  id;
    private Integer type;
    private String  name;

    // getters & setters...
}

我尝试了以下别名来解析 XML

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);

但是我不知道如何设置loc"标签的值,如果我尝试在 XML 中转换 City 对象,我会得到

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>

当我真的需要得到这个

<search>
    <loc id="ARBA0001" type="1">Buenos Aires</loc>
</search>

然后,如果我尝试将 XML 解析为 City 对象,我会得到带有空值的字段name".

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.

推荐答案

我终于找到了解决方案,一个Converter解决了这个问题,这是代码

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);
    }

}

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

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);

一切正常:)

这篇关于XStream 同时解析属性和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆