我的XML结构如下:
<Groups>
<Products>
<Product>
<Name>One</Name>
</Product>
<Product>
<Name>Two</Name>
</Product>
</Products>
<OtherProducts>
<Product>
<Id>1</Id>
</Product>
<Product>
<Id>2</Id>
</Product>
</OtherProducts>
</Groups>
我试图使用XStream解析这个,使用以下类:
@XStreamAlias("Groups")
class GroupData {
List<Product> Products;
List<OtherProduct> OtherProducts;
}
@XStreamAlias("Product")
class Product {
String name;
}
@XStreamAlias("Product")
class OtherProduct {
int id;
}
其中存在问题 - 解析器尝试使用“OtherProduct”类转换“Product”项。
我相信必须有一些方法来指定用于解析XML对象的类,但是我无法对XStream属性进行反驳。
非常感谢任何帮助。
答案 0 :(得分:2)
解决方案并非直截了当。
marshal(Object to XML)非常简单,当你必须解组(XML to Object)时会出现问题。
当XStream开始读取XML并找到Product标签时,它无法确定它是“Product”还是“OtherProduct”对象,因为标签名称是相同的。
所以,你必须教XStream向前看并检查你知道的让它们与众不同的东西。在这种情况下,内部属性“名称”和“Id”。
您可以教XSTream如何编写转换器。
上面的解决方案展示了如何解决问题。
产品类别:
@XStreamAlias("Product")
public class Product {
@XStreamAlias("Name")
private String name;
public Product() {
}
public Product(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
OtherProduct类:
@XStreamAlias("Product")
public class OtherProduct {
@XStreamAlias("Id")
private int id;
public OtherProduct() {
}
public OtherProduct(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
GroupData类:
@XStreamAlias("Groups")
public class GroupData {
@XStreamAlias("Products")
private List<Product> products = new ArrayList<>();
@XStreamAlias("OtherProducts")
private List<OtherProduct> otherProducts = new ArrayList<>();
public void add(Product product) {
getProducts().add(product);
}
public void add(OtherProduct otherProduct) {
getOtherProducts().add(otherProduct);
}
public List<Product> getProducts() {
return products;
}
public List<OtherProduct> getOtherProducts() {
return otherProducts;
}
}
ProductConverter类:
public class ProductConverter implements Converter {
private ProductUnmarshaller productUnmarshaller = new ProductUnmarshaller();
@Override
public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
return clazz.equals(Product.class);
}
@Override
public void marshal(Object object, HierarchicalStreamWriter hsw, MarshallingContext mc) {
Product product = (Product) object;
hsw.startNode("Name");
hsw.setValue(product.getName());
hsw.endNode();
}
@Override
public Object unmarshal(HierarchicalStreamReader hsr, UnmarshallingContext uc) {
return productUnmarshaller.unmarshal(hsr, uc);
}
}
OtherProductConverter类:
public class OtherProductConverter implements Converter {
private ProductUnmarshaller productUnmarshaller = new ProductUnmarshaller();
@Override
public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
return clazz.equals(OtherProduct.class);
}
@Override
public void marshal(Object object, HierarchicalStreamWriter hsw, MarshallingContext mc) {
OtherProduct otherProduct = (OtherProduct) object;
hsw.startNode("Id");
hsw.setValue(Integer.toString(otherProduct.getId()));
hsw.endNode();
}
@Override
public Object unmarshal(HierarchicalStreamReader hsr, UnmarshallingContext uc) {
return productUnmarshaller.unmarshal(hsr, uc);
}
}
ProductUnmarsheller类:
public class ProductUnmarshaller {
public Object unmarshal(HierarchicalStreamReader hsr, UnmarshallingContext uc) {
hsr.moveDown();
String nodeName = hsr.getNodeName();
String nodeValue = hsr.getValue();
hsr.moveUp();
if ("Name".equals(nodeName)) {
return new Product(nodeValue);
} else if ("Id".equals(nodeName)) {
return new OtherProduct(Integer.parseInt(nodeValue));
} else {
return null;
}
}
}
最后,一个使用它的课程:
public class ProductTest {
@Test
public void test() {
Product productOne = new Product("One");
Product productTwo = new Product("Two");
OtherProduct otherProduct1 = new OtherProduct(1);
OtherProduct otherProduct2 = new OtherProduct(2);
GroupData group = new GroupData();
group.add(productOne);
group.add(productTwo);
group.add(otherProduct1);
group.add(otherProduct2);
XStream xs = new XStream();
xs.processAnnotations(GroupData.class);
xs.processAnnotations(OtherProduct.class);
xs.processAnnotations(Product.class);
xs.registerConverter(new ProductConverter());
xs.registerConverter(new OtherProductConverter());
String xml = xs.toXML(group);
System.out.println(xml);
GroupData gd = (GroupData) xs.fromXML(xml);
for (Product product: gd.getProducts()) {
System.out.println(product.getName());
}
for (OtherProduct otherProduct: gd.getOtherProducts()) {
System.out.println(otherProduct.getId());
}
}
}