2010-06-28 23 views
5

Böyle bir şeye benzeyen bir XML yükü alıyorum (daha kapsamlı bir örnek için şu adrese göz atın: http://api.shopify.com/product.html).XStream - Nesnelerin koleksiyonu olarak kök

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

Şimdi şu anda benim kod çalışır, ancak onun gerçekten çok "yanlış" olarak görünmektedir şey yapıyor - yani o List.class ile "ürünler" ilişkilendirir. Yani ilgili kod aşağıdaki gibi görünür:

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

Bu benim git o zaman ne istediğimi değil elbette "ürünlerini", kullandığı xstream örneği ile herhangi bir nesneyi dışa dışında gayet iyi.

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

I: Şu anda değil, hangi çalışmak için aşağıdaki Snippet

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

Veya olsun:

Ben bir etikete jenerik koleksiyonları harita edebilmek ya istiyorum ShopifyProductResponse sınıfını denemek ve sarmak için ShopifyProductResponse sınıfını yarattı ancak bunun bana şu anlama gelmediğini bildirdi:

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

Ben eklerseniz:

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

o zaman ortadan kayboluyor ... bu yüzden mapperwrapper'ın burada tutulmadığı görülüyor - muhtemelen ShopifyProductResponse nesnesini aradığı ve bunun yerine bir liste bulduğu için - gerçekten bilmiyorum.

cevap

6

Doğru anlıyorsam, aradığınız şey budur. 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; 
} 

}

Ve bunun için bir dönüştürücü. UnMarshalling buna benzeyebilir.

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

Ve olarak tescil edebilir

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

Bunu denedim ve oldukça fazla para cezası çalışır. Bir şans ver ve bana haber ver.

+0

İyi görünüyor, bunun için etrafta asılı fazladan bir sarmalayıcı nesnesi olması fikrini pek sevmiyorum ama amaçlandığı gibi çalışıyor ve bu noktada gerçekten önemli olan şey - teşekkürler! – Lypheus