luckyreed76
luckyreed76

Reputation: 185

How to get XML content with matching tags in Pull Parser for Android?

I am trying to use the Pull Parser in Android to get values of image2 and image3.

<item>
<title>Title Goes Here!</title>
<picture><![CDATA[http://blahblah.image.jpg]]></picture>
<picture><![CDATA[http://blahblah.image2.jpg]]></picture>
<picture><![CDATA[http://blahblah.image3.jpg]]></picture>
</item>


...
case XmlPullParser.END_TAG
if (tagname.equalsIgnoreCase("item")
_feed.addItem(_item);
} else if (tagname.equalsIgnoreCase("title")) {
_item.setTitle(theString)
} else if (tagname.equalsIgnoreCase("picture")) {
_item.setLargeImage(theString)
} else if (tagname.equalsIgnoreCase("picture")) {
_item.setLargeImage2(theString)
} else if (tagname.equalsIgnoreCase("picture")) {
_item.setLargeImage3(theString)
}
break;
...

I can parse and load the first image but, I don't know what the next step is to get the other images? Thanks for the help.

Upvotes: 2

Views: 476

Answers (1)

neevek
neevek

Reputation: 12148

There's a logic error in your code:

} else if (tagname.equalsIgnoreCase("picture")) {
    _item.setLargeImage(theString)
} else if (tagname.equalsIgnoreCase("picture")) {
    _item.setLargeImage2(theString)
} else if (tagname.equalsIgnoreCase("picture")) {
    _item.setLargeImage3(theString)
}

The code that follows the second and third else if condition test will never be executed, if the encountered tag is picture, the control flow will end at the first if (tagname.equalsIgnoreCase("picture")).

You may do the following to achieve what you want:

List<String> imageList = new ArrayList<String>();
} else if (tagname.equalsIgnoreCase("picture")) {
    imageList.add(theString);
}

// after you finish parsing the xml file, set the image URLs on your item.
_item.setLargeImage(imageList.get(0));
_item.setLargeImage2(imageList.get(1));
_item.setLargeImage3(imageList.get(2));

Upvotes: 2

Related Questions