• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Get the arrtibute value using hashmap in android xml parsing

krishnaveni

Well-Known Member
Hi.,

I have to develop one android example.here i have to pass category name and article title.

[HIGH]
<root> <Categories> <Category name="book"> <Articles> <article articleid="170" title="java programming"> <thumb_image> <image url="http://website/var/tmp/thumb_2536_design_pavitra-rajaram_1_thumb_dec2012__forfeed.jpeg"/> </thumb_image> <images> <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> </images> </article> <article articleid="171" title="Android programming"> <thumb_image> <image url="http://website/var/tmp/thumb_2220_1_itc-grand-chola-chennai_main_dec2012__forfeed.jpeg"/> </thumb_image> <images> <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> </images> </article> </Article> </Category> <Category name="Animals">
<Articles> <article articleid="81" title="Dog"> <thumb_image> <image url="http://website/var/tmp/thumb_2220_1_itc-grand-chola-chennai_main_dec2012__forfeed.jpeg"/> </thumb_image> <images> <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> </images> </article> <article articleid="81" title="Cat"> <thumb_image> <image url="http://website/var/tmp/thumb_2220_1_itc-grand-chola-chennai_main_dec2012__forfeed.jpeg"/> </thumb_image> <images> <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> </images> </article> </Article> </Category> </Categories>
[/HIGH]


Here i have to display the category name and article title name..

I have used below code:
[HIGH]
public class MainActivity extends Activity {
static final String URL = "http://dev2.mercuryminds.com/webservices/new_feed_articls.xml";
static String KEY_CATEGORY = "Categories";
static final String KEY_TITLE = "Category";
static final String KEY_TITLENAME = "name";
static final String KEY_ARTICLE = "article";
static final String KEY_ARTICLETITLE = "title";
public static ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_TITLE);

// looping through all song nodes &lt;song&gt;
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String> ();
Element e = (Element) nl.item(i);
map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute(KEY_TITLENAME));
songsList.add(map);
}
GridView list = (GridView) findViewById(R.id.listView1);
adapter = new LazyAdapter(this, songsList);
list.setAdapter(adapter);
[/HIGH]

Here i have to run the app means am getting category name...but i didn't get article name..because article title is i didn't add here.How can i add the article title on hashmap..please give me solution for these..
 
Back
Top Bottom