简体   繁体   中英

Android Java Passing XML parser

I've written a program to get data from my XML file in Eclipse, and I've printed to a text view to ensure that the try is working, but it's not passing the data, but I'm unsure as to whether I've got the wrong parameters or I am calling it incorrectly so any help would be greatly appreciated.

public class AnimalList extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_animal_list);

    //pulls data from XML file
    XmlPullParserFactory pullParserFactory;
    try 
    {
        pullParserFactory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = pullParserFactory.newPullParser();

            InputStream in_s = getApplicationContext().getAssets().open("animals.xml");
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in_s, null);

            parseXML(parser);

            TextView display = (TextView)findViewById(R.id.animalInfo);
            display.setText("crap");

    } 
    catch (XmlPullParserException e) 
    {
        e.printStackTrace();


    } 
    catch (IOException e) 
    {
        //Auto-generated catch block
        e.printStackTrace();


    }
}

I know the above is working by printing to a text view but next i have

private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
    ArrayList<Animal> animals = new ArrayList<Animal>();
    int eventType = parser.getEventType();
    Animal currentAnimal = null;


    while (eventType != XmlPullParser.END_DOCUMENT)
    {

        String name = null;
        switch (eventType)
        {
            case XmlPullParser.START_DOCUMENT:
                //animals = new ArrayList();
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();
                if (name == "animal")
                {
                    currentAnimal = new Animal();
                } 
                else if (currentAnimal != null)
                {
                    if (name == "specificLocation")
                    {
                        currentAnimal.specificLocation = parser.nextText();
                    } 
                    else if (name == "name"){
                        currentAnimal.name = parser.nextText();
                    } 
                    else if (name == "location")
                    {
                        currentAnimal.location= parser.nextText();
                    }  
                    else if (name == "image")
                    {
                        currentAnimal.image= parser.nextText();
                    }  
                    else if (name == "animalInfo")
                    {
                        currentAnimal.animalInfo= parser.nextText();
                    }  
                    else if (name == "todaysFeed")
                    {
                        currentAnimal.todaysFeed= parser.nextText();
                    }  
                }
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("animal") && currentAnimal != null){
                    animals.add(currentAnimal);
                } 
        }
        eventType = parser.next();
    }

    printAnimals(animals);
}

And then the code within it isn't doing anything, so hoping someone can point out if I'm passing it correctly? Within the second it's just putting data into an array, but if I put a text view output before I do anything in it, it's not doing anything which makes me think it's not passing data.

Thanks.

Searching tags names You have compared references, but not strings. Its works fine besides. So parseXML will be like that:

private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
    ArrayList<Animal> animals = new ArrayList<Animal>();
    int eventType = parser.getEventType();
    Animal currentAnimal = null;


    while (eventType != XmlPullParser.END_DOCUMENT)
    {

        String name = null;
        switch (eventType)
        {
            case XmlPullParser.START_DOCUMENT:
                //animals = new ArrayList();
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase( "animal"))
                {
                    currentAnimal = new Animal();
                } 
                else if (currentAnimal != null)
                {
                    if (name.equalsIgnoreCase( "specificLocation"))
                    {
                        currentAnimal.specificLocation = parser.nextText();
                    } 
                    else if (name .equalsIgnoreCase( "name")){
                        currentAnimal.name = parser.nextText();
                    } 
                    else if (name .equalsIgnoreCase( "location"))
                    {
                        currentAnimal.location= parser.nextText();
                    }  
                    else if (name .equalsIgnoreCase( "image"))
                    {
                        currentAnimal.image= parser.nextText();
                    }  
                    else if (name .equalsIgnoreCase(  "animalInfo"))
                    {
                        currentAnimal.animalInfo= parser.nextText();
                    }  
                    else if (name .equalsIgnoreCase(  "todaysFeed"))
                    {
                        currentAnimal.todaysFeed= parser.nextText();
                    }  
                }
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("animal") && currentAnimal != null){
                    animals.add(currentAnimal);
                } 
        }
        eventType = parser.next();
    }

    printAnimals(animals);
}   

hope it helps :) Marcin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM