0
<GetProductsForMachine>
<Products>
    <ProductsForMachine>coffee espresso</ProductsForMachine>
    <ProductsForMachine>coffe1</ProductsForMachine>
    <ProductsForMachine>coffee2</ProductsForMachine>

</Products>

What must to be the implementation of Class ProductsForMachine,it's difficult because there isn't elements to get theirs value.

I try with following code but i have errors in the parsing..

@Root
public class ProductsForMachine{

    @Attribute(name="ProductsForMachine", required=true)
    public String ProductsForMachine;



    @Element(required=false)
    public int value;
}

Thanks

IMSoP
  • 89,526
  • 13
  • 117
  • 169
user1106234
  • 223
  • 4
  • 15
  • `it's difficult because there aren't elements to get theirs value.` Why? `ProductsForMachine`has values in your XML file. Or did I misunderstood you? – Ahmad Sep 18 '12 at 23:11
  • okay please tell me what must be the class for Parsing it because the class which i write doesen't work in parsing.. – user1106234 Sep 18 '12 at 23:14

2 Answers2

1

Here's a simple solution assuming that each <ProductsForMachine>...</ProductsForMachine> entry can be represented by a String.

@Root(name="GetProductsForMachine")
public class GetProductsForMachine
{
    @ElementList(name="Products", entry="ProductsForMachine")
    private ArrayList<String> products; /* 1 */


    // Construtors / Methods

}

Note 1: For the reason why ArrayList<String> is used instead of List<String> please see here.

It's possible to rename the class / field since the name value of the Annotations is set.

You now can deserialize your XML like this:

File f = // your xml file

Serializer ser = new Persister();
GetProductsForMachine products = ser.read(GetProductsForMachine.class, f);

Btw. GetProductsForMachine requires a closing tag.

Community
  • 1
  • 1
ollo
  • 24,797
  • 14
  • 106
  • 155
0

Have a look at my answer to this question. It provides a general approach to parsing simple xml when looking for a specific tag.

Community
  • 1
  • 1
swhitewvu24
  • 284
  • 1
  • 5