如何将XML文件中的数据添加到ArrayList

时间:2012-04-02 17:36:45

标签: java android arraylist

这是一个Android应用程序,我有一个我想读的名为'categories.xml'的XML文件:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<categories>
  <category>
    <name>Simulator Commands</name>
    <id>1</id>
  </category>
  <category>
    <name>Control Surface Commands</name>
    <id>2</id>
  </category>
  <category>
    <name>Control Surface Commands</name>
    <id>3</id>
  </category>
  <category>
    <name>General Aircraft Commands</name>
    <id>4</id>
  </category>
  <category>
    <name>Light Commands</name>
    <id>5</id>
  </category>
  <category>
    <name>Radio Commands</name>
    <id>6</id>
  </category>
  <category>
    <name>Autopilot Commands</name>
    <id>7</id>
  </category>
  <category>
    <name>Intrument Commands</name>
    <id>8</id>
  </category>
  <category>
    <name>View Commands</name>
    <id>9</id>
  </category>
  <category>
    <name>Slew Keys</name>
    <id>10</id>
  </category>
  <category>
    <name>Mission Commands</name>
    <id>11</id>
  </category>
  <category>
    <name>Multiplayer Commands</name>
    <id>12</id>
  </category>
  <category>
    <name>C172 Intrument Panels</name>
    <id>13</id>
  </category>
  <category>
    <name>BE58 Intrument Panels</name>
    <id>14</id>
  </category>
</categories>

我想将tag中的文本添加到名为'categorys'的ArrayList中。我试过这个:

ArrayList<String> categorys = new ArrayList<String>();
        categorys.add("Please select a category");
        //START ADDING HERE
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();
        File file = new File(Environment.getExternalStorageDirectory()+ "/fsx_kneeboard/categories.xml");
        FileInputStream fis = new FileInputStream(file);
        parser.setInput(new InputStreamReader(fis));
        parser.next();
        int eventType = parser.getEventType();
        int Categories = 0;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                Categories++;
            } if (eventType == XmlPullParser.START_TAG && Categories == 1) {
                Categories++;
            } if (eventType == XmlPullParser.START_TAG && Categories == 2) {
                categorys.add(parser.getText());
            }
            parser.next();
        }

但是这一部分有些东西:

while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    Categories++;
                } if (eventType == XmlPullParser.START_TAG && Categories == 1) {
                    Categories++;
                } if (eventType == XmlPullParser.START_TAG && Categories == 2 && parser.getName.equals("name")) {
                    categorys.add(parser.getText());
                }
                parser.next();
            }

强行关闭,我确定这部分是错误所在,因为如果我删除它就不会强制关闭,只会在ArrayList中添加“请选择一个命令”。

  

感谢您的时间和帮助,zeokila

1 个答案:

答案 0 :(得分:1)

关键是这一行......

    int eventType = parser.getEventType();

...因为它在while {...}循环之外且永远不会改变。

这意味着while条件eventType != XmlPullParser.END_DOCUMENT永远不会为假,因此您最终会反复调用parser.next()强制它过去文件结束可能导致异常。

    int eventType = parser.getEventType();
    int Categories = 0;
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
            Categories++;
        } if (eventType == XmlPullParser.START_TAG && Categories == 1) {
            Categories++;
        } if (eventType == XmlPullParser.START_TAG && Categories == 2) {
            categorys.add(parser.getText());
        }
        parser.next();
    }

每次致电eventType后,您需要更新parser.next(),我怀疑您在每次致电Categories后都希望将categorys.add(...)重置为0