I have parsed an XML feed, but within one of the tags I wish to further parse the data, but am unsure of how to do this? I'm utilising XMLPullParser for this in a package, then the information is stored in a Widgetclass package, before being sent to my mainactivity through an inent using a broadcast receiver.
currently the code is only calling one field of the parsed information, but calling more presents the problem of the unsorted XML Tag's contents. I cant seem to get a split.String method to work here, should it be in the widget class I've got to store the information or in the mainactivity?
//the parser public static WidgetClass[] parseFeed(String content) {
try {
boolean inItemTag = false;
String currentTagName = "";
WidgetClass currentItem = null;
List<WidgetClass> itemList = new ArrayList<>();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(new StringReader(content));
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
currentTagName = parser.getName();
if (currentTagName.equals("item")) {
inItemTag = true;
currentItem = new WidgetClass();
itemList.add(currentItem);
}
break;
case XmlPullParser.END_TAG:
if (parser.getName().equals("item")) {
inItemTag = false;
}
currentTagName = "";
break;
case XmlPullParser.TEXT:
String text = parser.getText();
if (inItemTag && currentItem != null) {
try {
switch (currentTagName) {
case "title":
currentItem.setTitle(text);
break;
case "description":
currentItem.setDescription(text);
break;
case "link":
currentItem.setLink(text);
break;
case "pubDate":
currentItem.setPubDate(text);
break;
case "category":
currentItem.setCatagory(text);
break;
case "geo:lat":
currentItem.setGeoLat(Double.parseDouble(text));
case "geo:long":
currentItem.setGeoLong(Double.parseDouble(text));
default:
break;
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
break;
}
eventType = parser.next();
} // end while loop
WidgetClass[] widgetClasses = new WidgetClass[itemList.size()];
return itemList.toArray(widgetClasses);
} catch (Exception e) {
e.printStackTrace();
return null;
}
Ideally I'd like to further split the stream from the tag, then put them into the array to pass on to my visual layer.
Your string example:
As you can see, all parts of strings are separated by ';'. In this case, you can use this char to split the string:
Then, each part contains the desired part of the string
If you are editing also the
xmlfile, I recommend you to edit it like thisSo, you can access each part directly with
XmlPullParser