0

I am reading from an XML file using XML Parser.

XML Parser is reading the first line as null and then returns other data from the xml file so created.

Following is the XML Parsing Code:

XmlResourceParser parser = getResources().getXml(R.xml.contacts);

        int eventType = -1;
        try {
            while (eventType != XmlResourceParser.END_DOCUMENT) 
            {
                if(eventType == XmlResourceParser.START_TAG) 
                {
                    if(parser.getName().toString().equals("contact") && parser.getName().toString().equals("null"))
                        col.append(parser.getAttributeValue(null, "name") + "\n");
                        col.append(parser.getAttributeValue(null, "phone") + "\n \n");
                }
                eventType = parser.next();
            }
        }
        catch (XmlPullParserException e) {

            e.printStackTrace();
        }

Following is the XML File:

<?xml version="1.0" encoding="utf-8"?>
<contacts>
    <contact name="Prathama Blood Center" phone="079 26600101" />
    <contact name="Green Cross" phone="079 25507013"/>
    <contact name="Crossworld Blood Bank" phone="079 26568004"/>
    <contact name="Adarsh Voluntary Blood Bank" phone="079 22746672"/>
    <contact name="Red Cross" phone="079 27551790"/>
</contacts>

Following is the Screen I am getting:

enter image description here

Rushabh
  • 385
  • 2
  • 5
  • 14
  • 1
    `parser.getName().toString().equals("contact") && parser.getName().toString().equals("null")` i don't think this will ever return `true`? – Aprian Sep 01 '12 at 03:24

1 Answers1

2

Right now the scope of your if only extends to the first col.append I'm not sure why you need that if statement (because its never true) Here's what the scope should be:

if(parser.getName().toString().equals("contact") && parser.getName().toString().equals("null"))
{ //here
    col.append(parser.getAttributeValue(null, "name") + "\n");
    col.append(parser.getAttributeValue(null, "phone") + "\n \n");
}//and here 
Nate-Wilkins
  • 5,364
  • 4
  • 46
  • 61