我要解析的 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 中取出的问题存储在我创建的数组列表中。我也在努力确保这将存储多个问题。非常感谢你甚至看这个哈哈。