I have been looking around for answers to this question and looked at the docs, and found them hard to follow, and couldn't apply it to my question.
I have also looked at this question on stack how do I extract text from a nested xml using xmlpullparser in android?. But there must be an easier, more concise and code efficient way than uses its method.
I have this xml which I get from the web and have all set up and now I want to display it in a list view as my end result so ideally I want to get this data into an array so I can use an ArrayAdapter to get it in the list view. I think that would be the simplest way. Here is the xml.
<menu>
<day name="monday">
<meal name="LUNCH">
<counter name="Deli">
<name>Made to Order Deli Core</name>
</counter>
</meal>
<meal name="DINNER">
<counter name="Entree">
<name>Turkey</name>
</counter>
</meal>
</day>
<day name="tuesday">
<meal name="LUNCH">
<counter name="Entree">
<name>Chicken</name>
</counter>
</meal>
</day>
<day name="wednesday">
<meal name="DINNER">
<counter name="Deli">
<name>Tuna Sandwich </name>
</counter>
</meal>
</day>
<menu>
So what I want this is return is a Dictionary of Arrays (an array of arrays) something that would look like this
Array[] array = [["LUNCH", "DINNER"], ["LUNCH"], ["DINNER"]];
And then I would want to display this in a list view. Here is what I have so far, I have all the set up to be able to parse but not too far with the actual parsing.
private int processReceivedData(XmlPullParser xmlData) throws XmlPullParserException, IOException {
String day = "";
while (eventType != XmlResourceParser.END_DOCUMENT) {
String tagName = xmlData.getName();
switch (eventType) {
case XmlResourceParser.START_TAG:
if (tagName.equalsIgnoreCase("day")) {
day = xmlData.getAttributeValue(null, "name");
}
case XmlResourceParser.TEXT:
data += xmlData.getText();
break;
case XmlPullParser.END_TAG:
if (tagName.equals("day")) {
recordsFound++;
publishProgress(day);
}
}
eventType = xmlData.next();
}
And then this
@Override
protected void onProgressUpdate(String... values) {
if (values.length == 0) {
Log.i(TAG, "no data");
}
else {
String day = values[0];
}
super.onProgressUpdate(values);
}
Right now this just gets the days, and thats it...
I know this is a long question, and if there is a better way to format it than an array of arrays please let me know.
Thanks for the help in advance.
No comments:
Post a Comment