4

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

Community
  • 1
  • 1
Maxbester
  • 2,435
  • 7
  • 42
  • 70

4 Answers4

4

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.

MH.
  • 45,303
  • 10
  • 103
  • 116
1

Use simplexml for this. When xml gets complicated the best one i've found to use with android is simple xml. So in your case the code will be like this, i could not format it well enough, but it will help. Cons: you have to create bunch of classes (depending upon the xml structure) but once you create them it's really smooth sailing afterward. Also, add getters in the classes to assess the values. Hope this helps

import java.util.ArrayList;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;

@Root(name = "ANIMALS", strict = false)
@Namespace(reference = "whatever", prefix = "sumthing")
public class Animal {
@ElementList(required = false, inline = true)// this will work even if some other
// element gets added to your ANIMALS element.
private ArrayList<Animal> somename; // does not matter
}

@Root(name = "animal", strict = false)
@Namespace(reference = "whatever", prefix = "sumthing")
public class Animal {
@Element(required = false)// to make it work even when this element is not part of 
    the string
private String name;
@ElementList(required = false, inline = true)
private ArrayList<Color> colors;
}

@Element(name = "color")
@Namespace(reference = "whatever", prefix = "sumthing")
public class Color {
@Element(required = false)   
private String clr;
public String getColor(){
return clr; 
}
}
  • Finally I used XmlPullParser because it is a lot faster. On the emulator Simple XML took 50 sec and XmlPullParser only 6 sec! By the way, in your example, you are not obliged to create a color class. In the animal class just create: `@ElementList(required = false, inline = true, entry = "Color")ArrayList colors;` – Maxbester Apr 25 '12 at 07:30
  • Simple XML will be slow first time round because it dynamically has to build the schema from annotations. However next time round it will be much faster. – ng. Feb 21 '13 at 08:42
0

Firstly you need to justify by considering your application requirements, why you need to use SimpleXML in the first place. Why you are not using Java based XML parsing. Here are a list of options you can avail: Best XML parser for Java

Community
  • 1
  • 1
Muhammad Maqsoodur Rehman
  • 33,681
  • 34
  • 84
  • 124
  • As just I just commented above the problem is that the structure of this XML is quite complicated and can evolve in the future. I wanted an easy, fast and adaptive solution. But thanks anyway! – Maxbester Apr 24 '12 at 11:19
0

Maybe you'll change your mind when see how easy is XmlPullParser.

You can read about it here: http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

Example of XmlPullParser usage:

XmlPullParser parser = getResources().getXml(R.xml.yourxml);
try {
            while (parser.getEventType() != XmlPullParser.END_DOCUMENT){
                if(parser.getEventType() == XmlPullParser.START_TAG && 
                        parser.getName().equalsIgnoreCase("object")){

                            name = parser.getAttributeValue(null, "name");
                            site = parser.getAttributeValue(null, "site");
                            phone = parser.getAttributeValue(null, "phone");
                            adds = parser.getAttributeValue(null, "address");
                }
                parser.next();
            }
        }  catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84
  • The problem is that the structure of this XML is quite complicated and can evolve in the future. I wanted an easy, fast and adaptive solution. But thanks anyway! – Maxbester Apr 24 '12 at 11:18