0

我要解析的 XML 文件是

<?xml version="1.0" encoding="utf-8"?>
<items>
    <item>
        <question>Question?</question>
        <answer1>Answer 1 is fun</answer1>
        <answer2>Answer 2 is too</answer2>
        <answer3>Answer 3 is me</answer3>
        <answer4>Answer 4 no more</answer4>
        <correctanswer>Answer 1 is fun</correctanswer>
        <comment>Sent in by: Salamader Jack, I need help :(</comment>
    </item>
</items>

到目前为止,我编写的代码可能完全关闭,也可能不完全关闭。这是我第一次写这样的解析器。我被告知有很多方法,所以如果有其他建议,请随时告诉我!:)

package assinment1.sfapps.com.myapplication;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;


class question {
    public String question;
    public String answer1;
    public String answer2;
    public String answer3;
    public String answer4;
    public String comment;
    public String correctAnswer;

}

public class TriviaGame extends Activity {
    ArrayList<question> questions;
    question q;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trivia_game);

        TextView myXmlContent = (TextView)findViewById(R.id.tvfun);
        String stringXmlContent;
        try {
            stringXmlContent = getEventsFromAnXML(this);
            myXmlContent.setText(stringXmlContent);
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private String getEventsFromAnXML(Activity activity)
            throws XmlPullParserException, IOException
    {
        //StringBuffer stringBuffer = new StringBuffer();
        Resources res = activity.getResources();
        XmlResourceParser xpp = res.getXml(R.xml.questions_q);
        xpp.next();
        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT)
        {
            q = new question();
            if(eventType == XmlPullParser.START_DOCUMENT || (xpp.getName() == "items" || xpp.getName() == "item" ))
            {
              //  stringBuffer.append("--- Start XML ---");
            }
            else if(eventType == XmlPullParser.START_TAG)
            {
                if(){

                }
                if(xpp.getName() == "question"){
                    q.question = xpp.getText();
                }else if(xpp.getName() ==  "answer1"){
                    q.answer1 = xpp.getText();
                }else if(xpp.getName() ==  "answer2"){
                    q.answer2 = xpp.getText();
                }else if(xpp.getName() ==  "answer3"){
                    q.answer3 = xpp.getText();
                }else if(xpp.getName() ==  "answer4"){
                    q.answer4 = xpp.getText();
                }else if(xpp.getName() ==  "comment"){
                    q.comment = xpp.getText();
                }else if(xpp.getName() ==  "correctAnswer"){
                    q.correctAnswer = xpp.getText();
                }
                questions.add(q);
               // stringBuffer.append("\nSTART_TAG: "+xpp.getName());
            }
            eventType = xpp.next();

        }

        return q.toString();
    }

    private void printQ(ArrayList<question> questions) {
        //findViewById()
    }


    @Override
public boolean onOptionsItemSelected(MenuItem item){
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id=item.getItemId();

        //noinspection SimplifiableIfStatement
        if(id==R.id.action_settings){
        return true;
        }

        return super.onOptionsItemSelected(item);
        }
}

任何帮助都会很棒。我知道这段代码没有完成。它也崩溃了。我正在尝试将我从 XML 中取出的问题存储在我创建的数组列表中。我也在努力确保这将存储多个问题。非常感谢你甚至看这个哈哈。

4

2 回答 2

0

我认为在您的情况下,使用对象更方便,并且答案保存在数组中,然后您可以
使用simple-xml获得任意数量的答案

于 2015-03-02T01:44:36.133 回答
0

在我的例子中,MyQs 是一个全局数组。
如何从 XML 中获取问题(这种情况下存储在数组中)。如果您需要未分配,请尝试使用 ArrayList。

 public void getQuestionsfromXML() throws XmlPullParserException,
            IOException {
        String tagname = " ";
        XmlResourceParser xpp = getResources().getXml(R.xml.trivia);

        if (xpp == null) {

        } else {
            xpp.next();
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    tagname = xpp.getName();
                } else if (eventType == XmlPullParser.TEXT) {
                    setNextQuestion(tagname, xpp.getText());

                }
                eventType = xpp.next();

            }
        }
    }

 public void setNextQuestion(String tagname, String textin) {

        if (tagname.equals("question")) {
            myQs[myQsindex] = new myQuestion();
            myQs[myQsindex].Question = textin;

        }
        if (tagname.equals("answer1")) {
            myQs[myQsindex].Answer1 = textin;

        }
        if (tagname.equals("answer2")) {
            myQs[myQsindex].Answer2 = textin;

        }
        if (tagname.equals("answer3")) {
            myQs[myQsindex].Answer3 = textin;

        }
        if (tagname.equals("answer4")) {
            myQs[myQsindex].Answer4 = textin;

        }
        if (tagname.equals("correctanswer")) {
            myQs[myQsindex].correctAnswer = textin;

        }

        if (tagname.equals("comment")) {
            myQs[myQsindex].comment = textin;
            msg = "question-> " + myQs[myQsindex].Question + "\n" + "Answer:     " + myQs[myQsindex].correctAnswer + "Index: " + myQsindex;

            if (TotalNumberofQs < maxQs)// precaution here
            {
                myQsindex++;
                TotalNumberofQs++;
            }
        }

显示它显然需要更多代码,但基本思想是
采用以下代码并在需要时使用它来更新问题。

    private void displayQuestion() {
            //Text View
            question.setText(myQs[myQsindex].Question);

            //Text View Radio buttons
            Answer1.setText(myQs[myQsindex].Answer1);
            Answer2.setText(myQs[myQsindex].Answer2);
            Answer3.setText(myQs[myQsindex].Answer3);
            Answer4.setText(myQs[myQsindex].Answer4);
            correctAnswer = myQs[myQsindex].correctAnswer;
    }
于 2015-03-03T10:29:18.483 回答