First item
<item>
<title>...</title>
<link>...</link>
<guid>...</guid>
<description>...</description>
<pubDate>Thu, 31 Jan 2013 13:46:24 +0530</pubDate>
</item>
Second item
<item>
<title>...</title>
<link>...</link>
<guid>...</guid>
<description>...</description>
<enclosure url="http://entertainment.oneindia.in/img/2013/01/31-race-2-6-day-collection.jpg" type="image/jpeg" length="202500"/>
<pubDate>Thu, 31 Jan 2013 10:36:35 +0530</pubDate>
</item>
“item” are the elements in a xml. First item don’t have “enclosure” tag, while in second item, it is available. My DOMParser throws NPE, because the tag “enclosure” for which, it is searching is not available in first “item”. How can I skip when preferred tag is not available?
My code with NPE is
objItem.setImage(getAttributeVal("enclosure", eElement));
public String getAttributeVal(String tag,Element eElement)
{
String imgUrl=eElement.getElementsByTagName(tag).item(0).getAttributes().getNamedItem("url").getNodeValue();
return imgUrl;
}
You current code does not allow for missing instances. It always assumes that it is present.
Modify your function to check if it exists.