【问题标题】:XStream - Root as a collection of objectsXStream - 作为对象集合的根
【发布时间】:2011-03-09 08:26:41
【问题描述】:

我正在使用一个看起来像这样的 XML 有效负载(有关更全面的示例,请查看:http://api.shopify.com/product.html)。

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

现在我的代码确实可以工作,但它做的事情似乎真的很“错误”——即将“产品”与 List.class 相关联。所以相关代码如下:

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

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

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

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

或者让下面的代码片段起作用,目前还不行:

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

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

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

如果我添加:

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

然后它消失了......所以在我看来,mapperwrapper 没有在这里占据一席之地 - 可能是因为它正在寻找 ShopifyProductResponse 对象并找到一个 List 代替 - 我真的不知道。

【问题讨论】:

    标签: java xstream


    【解决方案1】:

    如果我理解正确,这就是您要找的。 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;
    }
    

    }

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

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

    我已经试过了,效果很好。试试看,然后告诉我。

    【讨论】:

    • 看起来不错,我不太喜欢为此使用额外的包装器对象的想法,但它可以按预期工作,这就是此时真正重要的 - 谢谢!
    猜你喜欢
    • 2014-10-03
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 2013-04-22
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多