2

I'm parsing a xml into an object using Simple Framework. The problem is that the xml has elements with the same name but in different paths.

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>

my class is mapped like this:

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

But during the deserialization, it gives me the exception:

  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

How can i map tags with the same name but in different paths?

if I serialize the product object it gives me the correct XML structure:

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

Im deserializing like this:

product = new Product();
InputStream in = res.getResponse();
Serializer serializer = new Persister();
serializer.read(product, in,false);

1 Answers1

3

Here's an example how to map class:

(Example-) Class:

@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 + '}';
    }
}

(I don't have the full implementation but i hope my example is similar)

Input 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>

Note the <product> </product> tag and the " for id-attribute (without them it can fail)

Test-Code:

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

Output:

Product{name= name , description= description , desc= desc }

(Blanks are caused by xml)

It looks like you want to serialize / deserialize a list so products should be a list (can be inlined) and product is the class from above.



Edit:

Product class:

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

}

Note: I used that inner class as helper to get proper xml structure for the image element.

Now, next there's a class that wraps around the Product. I implemted this as a map, hoewever, if there's only one product you can use a simple class instead the list.

Prestashop class:

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

}

Note: For an explanation why i don't use List<Product> here, please see this answer.

Test-Code:

Serializer ser = new Persister();

Prestashop shop = ser.read(Prestashop.class, f);
System.out.println(shop.first());

Input 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>

(2nd one of your question)

And last ...

Output:

Product{name=aaa, description=long, desc=short, idDefaultImage=path, price=10.0, id=1}
Community
  • 1
  • 1
ollo
  • 24,797
  • 14
  • 106
  • 155
  • Thanks for the answer, but it didnt work... it keeps giving the same error. The error is only given in the reading, if i write the xml structure is build correctly(see editted post). I cant find a way to make it work, is there any way around? – Felipe Mammoli May 08 '13 at 00:54
  • I see; please see my *edit* for a 2nd try and tell me your result :-) – ollo May 08 '13 at 21:01
  • @ollo Care to take a look at this one? http://stackoverflow.com/questions/31999265/parsing-xml-feed-die-with-element-is-already-used – zundi Aug 20 '15 at 15:29