使用 XStream 解析 - 空标签和集合 [英] Parsing with XStream - empty tags and collections

查看:114
本文介绍了使用 XStream 解析 - 空标签和集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XStream 将 XML 转换为域对象,但遇到了一个问题.省略一些细节,XML 如下所示:

I am using XStream to convert XML into domain objects, and have come by a problem. Omitting a few details, the XML looks like this :

<airport>
    <flights>
        <flight>.....</flight>
        <flight>.....</flight>
        <flight>.....</flight>
    </flights>
</airport>

可以有 0 到 N 个 flight 元素.飞行元素本身包含其他元素.我为机场、航班和航班创建了类,并使用 xstream.alias 函数注册了它们.

There can be 0 to N flight elements. The flight elements themselves contain other elements. I have created classes for airport, flights, and flight and registered them with the xstream.alias function.

xstream = new XStream();
xstream.alias("airport", AirportPojo.class);
xstream.alias("flights", FlightsPojo.class);
xstream.alias("flight", FlightPojo.class);
xstream.useAttributeFor(AirportPojo.class, "flights");
xstream.addImplicitCollection(FlightsPojo.class, "flights", FlightPojo.class);
AirportPojo airportPojo = (AirportPojo) xstream.fromXML(xml);

因此,转换后,这给了我一个包含 FlightsPojo 对象的 AirportPojo 对象,其中包含 FlightPojo 对象的集合.但是,当有 0 个飞行元素时,FlightPojos 的集合似乎是 null.我希望(并且更喜欢)列表被初始化,但其中包含零个元素.我怎么能做到这一点?请记住,我不能使用注释,因为这是一个遗留项目.

So, after converting, this gives me an AirportPojo object containing a FlightsPojo object, containing a collection of FlightPojo objects. However, when there are 0 flight elements it seems that the collection of FlightPojos is null. I would expect (and prefer) the list to be initialized but with zero elements in it. How could I accomplish this? Bear in mind that I cannot use annotations as this is a legacy project.

推荐答案

如何实现自定义转换器?

How about implementing a custom converter?

class FlightsConverter implements Converter {
    @Override
    public boolean canConvert(Class clazz) {
        return clazz.equals(FlightsPojo.class);
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        FlightsPojo flightsPojo = new FlightsPojo();
        flightsPojo.setFlights(new ArrayList<FlightPojo>());
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            FlightPojo flightPojo = (FlightPojo) context.convertAnother(flightsPojo, FlightPojo.class);
            flightsPojo.getFlights().add(flightPojo);
            System.out.println(reader.getValue());
            reader.moveUp();
        }
        return flightsPojo;
    }

    @Override
    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
        // todo...
    }
}

然后像这样挂钩:

    XStream xstream = new XStream();
    xstream.registerConverter(new FlightsConverter());
    xstream.alias("airport", AirportPojo.class);
    xstream.alias("flights", FlightsPojo.class);
    xstream.alias("flight", FlightPojo.class);
    xstream.useAttributeFor(AirportPojo.class, "flights");
    AirportPojo airportPojo = (AirportPojo) xstream.fromXML(xml);

希望这有帮助;)

这篇关于使用 XStream 解析 - 空标签和集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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