Sushan Ghimire
Sushan Ghimire

Reputation: 7567

How to get value of only one Element from an Xml file using XmlResourceParser on Android?

I'm using XmlResourceParser to extract values from elements. I used msgBody= xmlResourceParser.nextText(); which returns value from both Message element i.e i'm glad that you came to see me and Sorry wrong number. I want to get only one from the top, how can i get that? My xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Messages>
<Contact Name="Tomas" Number="079399877339">
<Message Date="30 Jan 2012" Time="23:36">i'm happy that you go to see her</Message>
<Message Date="31 Jan 2012" Time="11:34">this is a sample message</Message>
</Contact>
</Messages>

.getText() throws NullPointerException.

Upvotes: 1

Views: 453

Answers (1)

Dmytro Danylyk
Dmytro Danylyk

Reputation: 19796

Your implementation is wrong. To fix it:

  1. Use SAXparser to parse XML.
  2. Save all messages to a list (List<Message>)
  3. Sort your message list by date, and grab the latest.

Upvotes: 1

Related Questions