0

If I alter my Data.xml file to now contain two branches instead of one how do I go about catching the objects in an array?

Current MainActivity.java file:

    try {
        Serializer serializer = new Persister();
        AssetManager assetManager = getAssets();
        InputStream inputStream = assetManager.open("data.xml");
        Data d = serializer.read(Data.class, inputStream);
        System.out.println(d.getPokemon());
    } 
    catch (Exception e) {
        e.printStackTrace();
    }

Current Data.java file:

@Root(name="Data")
public class Data {

    @Element(name="pkmn")
    private Pokemon pokemon;

    public Pokemon getPokemon() {
        return pokemon;           
    }
}

Current Pokemon.java file:

public class Pokemon implements Serializable{


    @Element(name="nm")
    private String name;
    @Element(name="tp")
    private String type;
    @Element(name="ablt")
    private String abilities;
    @Element(name="wkns")
    private String weakness;
    @Element(name="img")
    private String image;

    public Pokemon(){}
    public Pokemon(String n, String t, String a, String w, String i){
        name = n;
        type = t;
        abilities = a;
        weakness = w;
        image = i;
    }
    public String toString() {...}

Current Data.xml file:

<?xml version = "1.0" encoding = "utf-8" ?>
<Data>
<pkmn> 
        <nm>Beedrill</nm> 
        <tp>bug</tp> 
        <ablt>swarm</ablt>
        <wkns>fire</wkns>
        <img>beedrill</img>
    </pkmn> 
<Data>

New Data.xml file:

<?xml version = "1.0" encoding = "utf-8" ?>
<Data>

    <pkmn> 
        <nm>Beedrill</nm> 
        <tp>bug</tp> 
        <ablt>swarm</ablt>
        <wkns>fire</wkns>
        <img>beedrill</img>
    </pkmn> 

    <pkmn> 
        <nm>Blastoise</nm> 
        <tp>water</tp> 
        <ablt>torrent</ablt>
        <wkns>electric</wkns>
        <img>blastoise</img>
    </pkmn> 
</Data>
4

1 回答 1

2

根据文档,您可以使用@ElementList[1] 或@ElementArray[2]。

  1. http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#list
  2. http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#array
于 2013-10-25T02:25:59.007 回答