Hashmap In Android Xml Parsing
I have to fetch the data from xml feed and display on android application. my xml feed is looking like:
Solution 1:
Map<String,List<String>> categoryToArticlesMap = newHashMap<String,List<String>>();
NodeListcategoryNd= doc.getElementsByTagName("Category");
for (inti=0; i < categoryNd.getLength(); i++)
{
Elemente= (Element) categoryNd.item(i);
StringcategoryName= e.getAttribute("name");
NodeListarticlesNd= e.getElementsByTagName("Articles");
Elemente1= (Element) articlesNd.item(0);
NodeListarticleNd= e1.getElementsByTagName("article");
List<String> articles = newArrayList<String>();
for (intj=0; j < articleNd.getLength(); j++) {
ElementarticleNode= (Element) articleNd.item(j);
StringarticleName= articleNode.getAttribute("title");
articles.add(articleName);
}
categoryToArticlesMap.put(categoryName, articles);
}
This will give you the required output.
Solution 2:
Here in this line map.put( KEY_ARTICLE,((Element)nl1.item(j)).getAttribute("title"));
you are over writing the previous value present in the map. Change ArrayList<HashMap<String, String>> songsList
to ArrayList<HashMap<String, List<String>>> songsList
and add each value to list and put it to the map.
Even if you do this you will get output as,
Books
Android Java PHP MySQL Dita Frieda Demi Shyamalan Krish
Names
Android Java PHP MySQL Dita Frieda Demi Shyamalan Krish
To get the desired output, you should consider the nodelist for article
under each Category
instead of getting all the article
tags in the document.
Post a Comment for "Hashmap In Android Xml Parsing"