My Android app reads a huge XML file using Simple XML. Obviously it takes a while. The fact is, at the moment, I just need to retrieve a single list of elements. Is there a way to force Simple XML not to read all other elements?
I do not want to use another library. I just wonder if this kind of advanced stuff is possible with Simple XML.
Thanks for the help.
EDIT
Using MH suggestion, the parsing still takes 50 sec…
So I would like to try the other solution from Ilya Blokh using XmlPullParser.
Here is the needed part of the XML file:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
....
<ANIMALS>
<animal>
<name>Dog</name>
<color>white</color>
<color>black</color>
<color>brown</color>
</animal>
<animal>
<name>Cat</name>
<color>white</color>
<color>black</color>
<color>gray</color>
<color>red</color>
</animal>
...
</ANIMALS>
...
</ROOT>
I would like to populate this class:
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Animals {
/**
* Key: animal name, Value: animal colors
*/
private Map<String, List<String>> entries;
public Animals() {
entries = new HashMap<String, List<String>>();
}
public void addAnimal(String name, List<String> colors) {
entries.put(name, colors);
}
public List<String> getColors(String animalName) {
return entries.get(animalName);
}
}
How could I deal with lists and XmlPullParser? I am not used to working with parsers…
Thanks again
You can use the loose mapping flag to get around the strict requirement of specifying the whole document structure. This feature might be especially convenient in your case if you use it directly on the
Persister, since you mention only being interested in a single list of elements. Have a look at and read through the example linked.Unfortunately I’m not familiar enough with the internals of Simple XML to say anything about “not to read all other elements”. I suppose the loose mapping option as described above will result in only the mapping you’re interested in, but I doubt you can avoid parsing the whole XML document.