Android Parse XML Attribute

Ask by : codegirl February 10, 2015 22:58

I am parsing two xml files - the first I have successfully parsed using XmlParser:-

public final String getElementValue(Node elem) {
    Node child;
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child
                    .getNextSibling()) {
                if (child.getNodeType() == Node.TEXT_NODE) {
                    System.out.println("±VALUE: "
                            + child.getNodeValue().toString()
                            + " node type: " + child.getNodeType());
                    return child.getNodeValue();

The next xml file however has attributes that I need. Can I use the same parser and if so how do I change it to read attributes? The file I want to read is - I need the Line name and Status Description.

I am using another class with the KEYs in - how do I explicitly say which attribute I need? i.e. the below:-

static final String KEY_TRANSPORT = "LineStatus"; // parent node
static final String KEY_LINENAME = "???";
static final String KEY_LINEDESC = "???";


View original question

Answer by : AerinxNovember 03, 2012 18:33

The thing about XML is that you need to read/parse the entire file in order to retrieve any information, you can't read just the bits you need. You should read the file once and store every info you may need inside another class so you can retrieve it later.

Here is how I learned to parse XML from my archives, however, I used a SAXParser. You need to set a boolean to true or false on startElement and endElement to track where on the XML document you are reading tags, then you override characters and recover the String there, storing it inside your holder class depending on which booleans are true, remember that booleans tell you where you are inside the document.

If you have a complex document and a lot of information you want retrieved the Handler can have a lot of sublevels and you'll need to doublecheck because its easy to be confused and make an error.


Alternatively and using XMLPullParser, you can go inside the tags using loops and then store the information when inside the tag you're looking for. I never used this method, though, so I can't compare the efficiency compared to first one. In any case you need to navigate through the tags until you are on the tag you're looking for. Here it is:

XmlPullParser tutorials;
tutorials = XmlPullParserFactory.newInstance().newPullParser();
tutorials.setInput(downloadPath.openStream(), null);

while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_TAG) {
        String tagName = tutorials.getName();
        if (tagName.equals("item")) {
            // inner loop looking for link and title
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if (tutorials.getName().equals("link")) {
                    } else if (tutorials.getName().equals(
                            "title")) {
                } else if (eventType == XmlPullParser.END_TAG) {
                    if (tutorials.getName().equals("item")) {
                        // save the data, and then continue with
                        // the outer loop
                eventType =;
    eventType =;
View original answer