I have this XML:
<menu>
<day name="monday">
<meal name="BREAKFAST">
<counter name="Bread">
<dish>
<name>Plain Bagel</name>
</dish>
<counter/>
<meal/>
<day/>
<day name="tuesday">
<meal name="LUNCH">
<counter name="Other">
<dish>
<name>Cheese Bagel</name>
</dish>
<counter/>
<meal/>
<day/>
<menu/>
Now here is what I am trying to do, if the day tag's attribute is equal to monday. And then meals tag attribute is equal to BREAKFAST
, then I want to get the counter's attribute. "Bread".
I have set up xml pull parser, but I am struggling getting this value. Here is what I have tried, I now I see that it can't and won't work... So any help on how I could set it up to do this would be great.
while (eventType != XmlResourceParser.END_DOCUMENT) {
String tagName = xmlData.getName();
switch (eventType) {
case XmlResourceParser.START_TAG:
if (tagName.equalsIgnoreCase("day")) {
if (xmlData.getAttributeValue(null, "name").equalsIgnoreCase(day)) {
if (tagName.equalsIgnoreCase("meal")) {
mealArray.add(xmlData.getAttributeValue(null, "name"));
Log.i(TAG, xmlData.getAttributeValue(null, "name"));
}
}
}
break;
case XmlResourceParser.TEXT:
break;
case XmlPullParser.END_TAG:
break;
}
eventType = xmlData.next();
}
Android XMLPullParser Example. Below is a simple example that parses student data present in xml format using XMLPullParser. 1. Create a new android project with package name com.androidxmlparser. 2. Create a folder raw under res. Add a file student.xml inside res/raw with following xml data.
The XMLPullParser is a simple and efficient parser method to parse the XML data when compared to other parser methods such as DOM Parser and SAX Parser. The XMLPullParser has a method next () that provides access to high-level parsing events.
The xml.sax package offers a decent event-based XML parser interface modeled after the original Java API. It’s somewhat limited compared to the DOM but should be enough to implement a basic XML streaming push parser without resorting to third-party libraries.
If you want to parse XML namespaces, then you’ll need to create and configure the SAX parser yourself with a bit of boilerplate code and also implement slightly different callbacks: These callbacks receive additional parameters about the element’s namespace.
You would need to add logic for parsing nested tags:
A very simple example to help you move ahead:
I parsed this String:
<menu><day name=\"monday\"><meal name=\"BREAKFAST\"><meal/><day/></menu>
Code:
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader("<menu><day name=\"monday\"><meal name=\"BREAKFAST\"><meal/><day/></menu>"));
int eventType = xpp.getEventType();
while (eventType != XmlResourceParser.END_DOCUMENT) {
String tagName = xpp.getName();
switch (eventType) {
case XmlResourceParser.START_TAG:
if (tagName.equalsIgnoreCase("day")) {
if (xpp.getAttributeValue(null, "name").equalsIgnoreCase("MONDAY")) {
int eventType2 = xpp.next();
while (eventType2 != XmlResourceParser.END_DOCUMENT) {
String tagName2 = xpp.getName();
switch (eventType2) {
case XmlResourceParser.START_TAG:
if (tagName2.equalsIgnoreCase("meal")) {
Log.i("tag", "meal: " + xpp.getAttributeValue(null, "name"));
}
break;
case XmlResourceParser.TEXT:
break;
case XmlPullParser.END_TAG:
break;
}
eventType2 = xpp.next();
}
}
}
break;
case XmlResourceParser.TEXT:
break;
case XmlPullParser.END_TAG:
break;
}
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
You see the difference, right?
I added this, basically, just after getting the day what I wanted. (In my case, hard coded String "Monday"
.)
int eventType2 = xpp.next();
And based upon that eventType2
, retrieved tagName2
which would be for "meal"
A better example to help you write your logic in a nice manner.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With