如何在 Android 应用程序中解析和显示来自 XML 的内容?
How do I parse and display things from an XML in an Android application?
我要解析的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 中取出的问题存储在我创建的数组列表中。我也在努力确保这将存储多个问题。
非常感谢你看到这个哈哈。
我认为在你的情况下使用对象更方便,答案保存在数组中,然后你可以有任意数量的答案
使用 simple-xml
MyQs 在我的例子中是一个全局数组。
如何从 XML 中获取问题(这种情况存储在数组中)..如果你需要一个 none 分配尝试一个 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;
}
我要解析的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 中取出的问题存储在我创建的数组列表中。我也在努力确保这将存储多个问题。 非常感谢你看到这个哈哈。
我认为在你的情况下使用对象更方便,答案保存在数组中,然后你可以有任意数量的答案
使用 simple-xml
MyQs 在我的例子中是一个全局数组。
如何从 XML 中获取问题(这种情况存储在数组中)..如果你需要一个 none 分配尝试一个 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;
}