user1106234
user1106234

Reputation: 223

Simple Xml Parsing error in Android

<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

Upvotes: 0

Views: 193

Answers (2)

ollo
ollo

Reputation: 25350

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.

Upvotes: 1

swhitewvu24
swhitewvu24

Reputation: 284

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

Upvotes: 0

Related Questions