[英]How can I add data from an XML file to an ArrayList
this is for an Android application, I have an XML file that I would like to read named 'categories.xml': 这是一个Android应用程序,我有一个XML文件,我想读取它的名称为'categories.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>
And I would like to add the text from tag to an ArrayList named 'categorys'. 我想将标签中的文本添加到名为“ categorys”的ArrayList中。 I've tried this:
我已经试过了:
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();
}
But something in this part: 但是这部分内容:
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();
}
Make it force close, I'm sure it's this part the error is in because if I remove it it will not force close and will only add "Please select a command" to the ArrayList. 使其强制关闭,我确定这是错误所在的部分,因为如果删除它,则不会强制关闭,而只会将“请选择命令”添加到ArrayList中。
Thanks for your time and help, zeokila
谢谢您的时间和帮助,zeokila
The key is going to be this line... 关键是这条线...
int eventType = parser.getEventType();
...because it is outside the while {...}
loop and it is never changing. ...因为它在
while {...}
循环之外,并且从未改变。
That means the while
condition eventType != XmlPullParser.END_DOCUMENT
will never be false so you end up repeatedly calling parser.next()
forcing it to past end-of-file probably causing an exception. 这意味着
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();
}
You need to update eventType
after each call to parser.next()
and I suspect you'd also want to reset Categories
to 0 after each call to categorys.add(...)
您需要在每次调用
parser.next()
之后更新eventType
,并且我怀疑您还希望在每次调用categorys.add(...)
之后将Categories
重置为0 categorys.add(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.