Android - Retrieve only some elements from an XML file using Simple XML

My Android application reads a huge XML file using Simple XML . Obviously, this takes some time. The fact is that at the moment I just need to get one list of items. Is there a way to make Simple XML not read all other elements?

I do not want to use another library. I'm just wondering if such advanced material is possible using Simple XML.

Thanks for the help.


EDIT

Using the MH clause , parsing still takes 50 seconds ...

So, I would like to try another solution from Ilya Bloch using XmlPullParser .

Here is the necessary 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>

:

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);
    }
}

XmlPullParser? ...

+3
4

, . , Persister, , . .

, Simple XML, - , " ". , , , , , , XML-.

+3

-, , , SimpleXML . XML Java. , : XML- Java

0

, , , XmlPullParser.

: http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

Usage example XmlPullParser:

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();
        }
0
source

Use simplexml for this. When xml gets complicated, the best one I found to use with Android is plain xml. So in your case, the code will be like this, I could not format it well enough, but it will help. Cons: you need to create a bunch of classes (depending on the xml structure), but once you create them, it will be really smooth. Also, add getters to classes to evaluate 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; 
}
}
0
source

All Articles