XStream - Root作为对象的集合 [英] XStream - Root as a collection of objects

查看:137
本文介绍了XStream - Root作为对象的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用类似于此的XML有效负载(有关更全面的示例,请查看: http://api.shopify.com/product.html )。

I'm consuming an XML payload that looks something like this (for a more comprehensive example, check out : http://api.shopify.com/product.html ).

<products type="array">
   <product>
      ...
   </product>
   <product>
      ...
   </product>
</products>

现在我的代码确实有效,但它做的事似乎真的错了 - 即它将产品与List.class相关联。所以相关代码如下所示:

Now currently my code does work, but its doing something that appears to be really really "wrong" - namely it associates the "products" with List.class. So the relevant code looks like the following:

    xstream.alias( "products", List.class );
    xstream.alias( "product", ShopifyProduct.class );

这很好,除非我转到使用该xstream实例外部化任何对象时它总是使用产品当然,这不是我想要的。

This is fine except when I goto externalize any object with that xstream instance it always uses "products" of course, which is not what I want.

我希望能够将通用集合映射到标签:

I'd like to either be able to map generic collections to a tag:

xstream.alias( "products", ( List<ShopifyProduct> ).class ); // way too easy 

或者让以下snipet工作,目前还没有:

Or get the following snipet to work, which does not at the moment:

    ClassAliasingMapper mapper = new ClassAliasingMapper( xstream.getMapper( ) );
    mapper.addClassAlias( "product", ShopifyProduct.class );
    xstream.registerLocalConverter( ShopifyProductResponse.class, "products", new CollectionConverter( mapper ) );

我创建了ShopifyProductResponse类来尝试包装ShopifyProduct,但它没有任何告诉我:

I created the ShopifyProductResponse class to try and wrap ShopifyProduct, but its not having any of that telling me:

com.thoughtworks.xstream.mapper.CannotResolveClassException:products:products
at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:68 )
at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)

com.thoughtworks.xstream.mapper.CannotResolveClassException: products : products at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:68) at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:38)

如果我添加:

xstream.alias( "products", List.class );

然后它就消失了......所以在我看来mapperwrapper没有抓住这里 - 可能是因为它寻找ShopifyProductResponse对象并找到了一个List而不是 - 我真的不知道。

back then it goes away ... so it appears to me that the mapperwrapper is not taking hold here - probably because its looking for a ShopifyProductResponse object and finding a List instead - I really don't know.

推荐答案

如果我理解的话没错,这就是你要找的。
ShoppifyProductResponse.java

If i understand correct, this is what you are looking for. ShoppifyProductResponse.java

public class ShoppifyProductResponse {

private List<ShoppifyProduct> product;

/**
 * @return the products
 */
public List<ShoppifyProduct> getProducts() {
    return product;
}

/**
 * @param products
 *            the products to set
 */
public void setProducts(List<ShoppifyProduct> products) {
    this.product = products;
}

}

还有一个转换器。 UnMarshalling可能看起来像这样。

And a converter for this. UnMarshalling might look like this.

public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    /**
     * Tune the code further..
     */
    ShoppifyProductResponse products = new ShoppifyProductResponse();
    List<ShoppifyProduct> lst = new ArrayList<ShoppifyProduct>();
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        ShoppifyProduct thisProduct = (ShoppifyProduct) context.convertAnother(products,
                ShoppifyProduct.class);
        lst.add(thisProduct);
        reader.moveUp();
    }
    products.setProducts(lst);
    return products;
}

您可以将其注册为,

    XStream stream = new XStream();
    stream.alias("products", ShoppifyProductResponse.class);
    stream.registerConverter(new ShoppifyConverter());
    stream.alias("product", ShoppifyProduct.class);

我试过这个并且它的工作原理非常好。试一试,让我知道。

I have tried this and it works pretty much fine. Give it a try and let me know.

这篇关于XStream - Root作为对象的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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