Reputation: 2753
this is for an Android application, I have an XML file that I would like to read named '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'. 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.
Thanks for your time and help, zeokila
Upvotes: 1
Views: 578
Reputation: 48871
The key is going to be this line...
int eventType = parser.getEventType();
...because it is outside the while {...}
loop and it is never changing.
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.
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(...)
Upvotes: 1