Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With SAX Parser, get an attribute's value

I am parsing XML from the web using Android. The code below shows a sample of the XML. The problem I'm having is I can't get the string value of the item tag. When I use name = attributes.getQName(i); it outputs the name, not the value of the attribute.

<weatherdata>
 <timetags>
  <item name="date">
   <value>20/04/2012</value>
   <unit/>
   <image/>
   <class>dynamic</class>
   <description>The current date</description>
  </item>
like image 397
carsey88 Avatar asked Apr 20 '12 08:04

carsey88


2 Answers

use

attributes.getValue(i);

instead of

attributes.getQName(i);

because as doc says :

getQName :Return an attribute's qualified (prefixed) name.

getValue : Look up an attribute's value by qualified (prefixed) name.

see this example for getting attribute name and value

like image 141
ρяσѕρєя K Avatar answered Oct 01 '22 14:10

ρяσѕρєя K


 @Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
     if(localName.equalsIgnoreCase("item")){
        //currentMessage.setMediaUrl(attributes.getValue(BaseFeedParser.Url));
                     String valueis=attributes.getValue("name")
    }
    super.startElement(uri, localName, qName, attributes);
}
like image 35
Herry Avatar answered Oct 01 '22 14:10

Herry