我正在编写Android测验应用。我想从XML文件中加载问题,将它们放入字符串中,然后将这些字符串用作按钮文本。
我在使用XML解析时遇到了麻烦。我使用XmlPullParserFactory
和XmlPullParser
。
我甚至试过XMLResourceParser
,但没有成功。我怎样才能正确地完成这个?
这是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();
}
这些是函数和辅助类:
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]);
}
以下是答案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>
答案 0 :(得分:1)
如何正确完成此操作?
这与您的XML解析问题没有直接关系,但如果您可以将答案xml转换为字符串数组格式并将它们放在res/values/arrays.xml
内,则可以省去解析XML的麻烦。在代码中,您可以使用R.array.<answer_name_id>
例如,
<answer1>
<text1>Zvonimir</text1>
<text2>Tomislav</text2>
<text3>Branimir</text3>
<text4>Viseslav</text4>
</answer1>
将是
<string-array name="answer_1">
<item>Zvonimir</item>
<item>Tomislav</item>
<item>Branimir</item>
<item>Viseslav</item>
</string-array>
在代码中,您只需访问数组
即可String[] options = getResources().getStringArray(R.array.answer_1);
由于您有许多数组,因此可以定义一个int数组来保存对所有数组的引用。
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}
在代码中,您只需从
循环或访问所需答案即可String[] options = getResources().getStringArray(ALL_ANSWERS[index]);
但是,如果您从其他地方下载XML,则无效。只有当您的所有答案都打包在应用程序中时,此功能才有效。