我正在使用Simple Framework将xml解析为对象。 问题是xml具有相同名称但位于不同路径的元素。
XML:
<prestashop>
<products>
<product>
<name>
<language id="1"> name </language>
</name>
<description>
<language id="1"> description </language>
</description>
<description_short>
<language id="1"> desc </language>
</description_short>
</product>
</products>
</prestashop>
我的课程映射如下:
@Root(name="prestashop")
public class Product{
@Element(name="language")
@Path("products/product/description_short[1]")
private String shortDesc;
@Element(name="language")
@Path("products/product/description[1]")
private String longDesc;
@Element(name="language")
@Path("products/product/name[1]")
private String name;
}
但是在反序列化期间,它给了我例外:
org.simpleframework.xml.core.PersistenceException:
Element 'language' is already used with @org.simpleframework.xml.Element(data=false, name=language, required=true, type=void)
on field 'name' private java.lang.String model.Product.name at line 8
如何映射具有相同名称但位于不同路径的标签?
如果我序列化产品对象,它会给我正确的XML结构:
<prestashop xmlns="http://www.w3.org/1999/xlink">
<products>
<product>
<description_short>
<language>short</language>
</description_short>
<id_default_image href="path"/>
<description>
<language>long</language>
</description>
<name>
<language>aaa</language>
</name>
<price>10.0</price>
<id>1</id>
</product>
</products>
</prestashop>
我像这样反序列化:
product = new Product();
InputStream in = res.getResponse();
Serializer serializer = new Persister();
serializer.read(product, in,false);
答案 0 :(得分:3)
以下是如何映射类的示例:
(示例 - )类:
@Root(name = "product")
public class Product
{
@Path(value = "name")
@Element(name = "language")
private String name;
@Path(value = "description")
@Element(name = "language")
private String description;
@Path(value = "description_short")
@Element(name = "language")
private String desc;
// ...
/* For testing only */
@Override
public String toString()
{
return "Product{" + "name=" + name + ", description=" + description + ", desc=" + desc + '}';
}
}
(我没有完整的实施,但我希望我的例子类似)
输入Xml:
<product>
<name>
<language id="1"> name </language>
</name>
<description>
<language id="1"> description </language>
</description>
<description_short>
<language id="1"> desc </language>
</description_short>
</product>
请注意<product> </product>
标记和"
id
属性(没有它们可能会失败)
<强>试验编号:强>
final File f = new File("test.xml"); // Input file
Serializer ser = new Persister();
Product p = ser.read(Product.class, f); // deserialize the class
System.out.println(p); // output - thats why i've implemented the 'toString()' method
<强>输出:强>
Product{name= name , description= description , desc= desc }
(空白由xml引起)
看起来您想序列化/反序列化列表,因此products
应该是一个列表(可以内联),product
是上面的类。
Product
课程:
@Root(name = "product")
public class Product
{
@Path(value = "name")
@Element(name = "language")
private String name;
@Path(value = "description")
@Element(name = "language")
private String description;
@Path(value = "description_short")
@Element(name = "language")
private String desc;
@Element(name = "id_default_image")
private AttributedElement idDefaultImage;
@Element(name = "price")
private double price;
@Element(name = "id")
private int id;
@Override
public String toString()
{
return "Product{" + "name=" + name + ", description=" + description
+ ", desc=" + desc + ", idDefaultImage=" + idDefaultImage
+ ", price=" + price + ", id=" + id + '}';
}
@Root(name = "AttributedElement")
static class AttributedElement
{
@Attribute(name = "href")
private String value;
public AttributedElement(String value)
{
this.value = value;
}
private AttributedElement()
{
/* Empty constructor required here */
}
@Override
public String toString()
{
return value;
}
}
}
注意: 我使用该内部类作为帮助来获取图像元素的正确xml结构。
现在,接下来有一个包围Product
的类。我将其实现为地图,hoewever,如果只有一个产品,您可以使用简单的类而不是列表。
Prestashop
课程:
@Root(name = "prestashop")
public class Prestashop
{
@ElementList(name = "products", empty = false, required = true)
private ArrayList<Product> products;
public Prestashop()
{
this.products = new ArrayList<>();
}
/* Some list methods */
public void add(Product p)
{
products.add(p);
}
public Product get(int index)
{
return products.get(index);
}
public Product first()
{
return products.get(0);
}
}
注意: 有关我在此处未使用List<Product>
的解释,请参阅this answer。
<强>试验编号:强>
Serializer ser = new Persister();
Prestashop shop = ser.read(Prestashop.class, f);
System.out.println(shop.first());
输入Xml:
<prestashop xmlns="http://www.w3.org/1999/xlink">
<products>
<product>
<description_short>
<language>short</language>
</description_short>
<id_default_image href="path"/>
<description>
<language>long</language>
</description>
<name>
<language>aaa</language>
</name>
<price>10.0</price>
<id>1</id>
</product>
</products>
</prestashop>
(你提问的第二个)
最后......
<强>输出:强>
Product{name=aaa, description=long, desc=short, idDefaultImage=path, price=10.0, id=1}