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>

1 Answer 1

2

Per the documentation, you can use either @ElementList [1] or @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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.