Android-将XML解析为ArrayList [英] Android - Parsing XML into an ArrayList

查看:200
本文介绍了Android-将XML解析为ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解析和显示非常简单的xml文件中的信息时遇到麻烦.

I'm having trouble parsing and showing information from a very simple xml file.

首先,我将xml文件存储在res/xml文件夹中.在该文件夹中,我有一个名为jokes.xml的xml文件,该文件的内容如下:

First of all, I'm storing the xml file in the res/xml folder. In the folder I have an xml file named jokes.xml, the content of the file is the following:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <joke>This is a good joke.</joke>
  <joke>This is a very nice good great joke.</joke>
  <joke>Oh Yeah!</joke>
</resources>

我想解析此文件,将所有笑话存储到arraylist中,然后随机显示一个.我的代码如下:

I want to parse this file, store all the jokes into an arraylist and the get a random one to show. My code is the following:

public class MainActivity extends Activity {

private TextView texto;
private int i = 0;
private String[] arrayJokes;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    texto = (TextView) findViewById(R.id.textView1);

    XmlResourceParser joke = getResources().getXml(R.xml.jokes);

    try {
        while( joke.getEventType() != XmlResourceParser.END_DOCUMENT ){
            if( joke.getEventType() == XmlResourceParser.START_TAG ){
                String s = joke.getName();

                if( s.equals("joke")){
                    arrayJokes[i] = joke.getAttributeValue(null, "joke");
                    i++;
                }
            }
        }
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //takes random id from the array
    Random generator = new Random();
    int rnd = generator.nextInt(arrayJokes.length);

    String j = arrayJokes[rnd];
    texto.setText(j);

}

}

问题是我只能得到一个空白页.对我在做什么错有任何想法吗?

The problem is that I only get a blank page. Any idea on what I'm doing wrong?

预先感谢, 克拉迪奥

推荐答案

您可以像这样分析您的笑话项目:

you can parse your joke items like this:

private ArrayList<String> parseJokes() {
            ArrayList<String> jokes = new ArrayList<String>();
            XmlResourceParser parser = getResources().getXml(R.xml.jokes);


            try {
                int eventType=parser.getEventType();
                while (eventType!=XmlPullParser.END_DOCUMENT) {
                    if(eventType==XmlPullParser.START_TAG){
                        if (parser.getName().equals("joke")) {
                            jokes.add(parser.nextText());
                        }
                    }

                    eventType= parser.next();
                }
            } catch (XmlPullParserException | IOException e) {
                Log.e("XmlPullParserException", e.toString());
            }
            parser.close();
            return jokes;
        }

主要思想是使用 next()方法在文档中推进解析器.

the main idea is to advance the parser across the document using the next() method.

这篇关于Android-将XML解析为ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆