user2622378
user2622378

Reputation: 1

Parsing xmls in android

I'm writing an Android quiz app. I want to load questions from an XML file, put them into strings, and then use those strings as button text.

I'm having trouble with the XML parse. I use XmlPullParserFactory and XmlPullParser. I have even tried XMLResourceParser, but to no success. How can I correctly accomplish this?

This is initialization in OnCreate:

XmlPullParserFactory pullParserFactory;
    try {
        pullParserFactory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = pullParserFactory.newPullParser();
        InputStream in_s = getApplicationContext().getAssets().open("new_anwers.xml");
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in_s, null);
        loadAnswers(parser);
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

These are the functions and a helper class:

void loadAnswers(XmlPullParser parser) throws XmlPullParserException,IOException
{
    ArrayList<Answer> answers = null;
    int eventType = parser.getEventType();
    Answer currAnswer = null;

    while (eventType != XmlPullParser.END_DOCUMENT) {
        String name = null;

        switch(eventType){
        case XmlPullParser.START_DOCUMENT:
            answers = new ArrayList();
            break;
        case XmlPullParser.START_TAG:
            name = parser.getName();
            if(name == "answer1") {
                currAnswer = new Answer();                  
            } else if (currAnswer != null) {
                if(name == "text1") {
                    currAnswer.text1 = parser.nextText();
                } else if(name == "text2") {
                    currAnswer.text2 = parser.nextText();
                } else if(name == "text3") {
                    currAnswer.text3 = parser.nextText();
                } else if(name == "text4") {
                    currAnswer.text4 = parser.nextText();
                }
            }
            break;
        case XmlPullParser.END_TAG:
            name = parser.getName();
            if (name.equalsIgnoreCase("answer1") && currAnswer!= null){
                answers.add(currAnswer);
            }
        }
        eventType = parser.next();
    }
    setBtnTxt(answers);
}

class Answer {
    String text1;
    String text2;
    String text3;
    String text4;
}

void setBtnTxt(ArrayList<Answer> answers) {     
    String[] txt = new String[4];
    Iterator<Answer> it = answers.iterator();
    while(it.hasNext())
    {
        Answer currAnswer = it.next();
        txt[1] = currAnswer.text1;
        txt[2] = currAnswer.text2;
        txt[3] = currAnswer.text3;
        txt[4] = currAnswer.text4;
    }

    Button odgA = (Button) findViewById(R.id.button_OdgA);
    Button odgB = (Button) findViewById(R.id.button_OdgB);
    Button odgC = (Button) findViewById(R.id.button_OdgC);
    Button odgD = (Button) findViewById(R.id.button_OdgD);      

    odgA.setText(txt[1]);
    odgB.setText(txt[2]);
    odgC.setText(txt[3]);
    odgD.setText(txt[4]);
}

Here is the answers XML:

<?xml version="1.0" encoding="UTF-8"?>
<new_answers>
<answer1>
    <text1>Zvonimir</text1>
    <text2>Tomislav</text2>
    <text3>Branimir</text3>
    <text4>Viseslav</text4>
</answer1>
<answer2>
    <text1>Stipe Mesic</text1>
    <text2>Ivo Josipovic</text2>
    <text3>Franjo Tuđman</text3>
    <text4>Mate Granic</text4>
</answer2>
<answer3>
    <text1>Hrvatski politicar</text1>
    <text2>Hrvatski akademik</text2>
    <text3>Hrvatski glazbenik</text3>
    <text4>Hrvatski branitelj</text4>
</answer3>
</new_answers>

Upvotes: 0

Views: 152

Answers (1)

Sundeep
Sundeep

Reputation: 1606

How can I correctly accomplish this?

This is not directly related to your question of XML parsing, but you can save yourselves the trouble of parsing XML if you can convert your answers xml to a string array format and put them inside res/values/arrays.xml. In the code you can access them using R.array.<answer_name_id>

For example,

<answer1>
    <text1>Zvonimir</text1>
    <text2>Tomislav</text2>
    <text3>Branimir</text3>
    <text4>Viseslav</text4>
</answer1>

will be

<string-array name="answer_1">
        <item>Zvonimir</item>
        <item>Tomislav</item>
        <item>Branimir</item>
        <item>Viseslav</item>
</string-array>

In code, you can simply access the array as

String[] options = getResources().getStringArray(R.array.answer_1);

Since you have many arrays, you can define an int array to hold references to all your arrays.

public static final int[] ALL_ANSWERS = {
        R.array.answer_1,
        R.array.answer_2,
        R.array.answer_3,
        R.array.answer_4,
        R.array.answer_5}

And in code you can simply loop or access your required answer from

String[] options = getResources().getStringArray(ALL_ANSWERS[index]);

However, if you are downloading the XML from some where else, this won't work. This works only if all your answers are packaged in the app.

Upvotes: 1

Related Questions