With SAX Parser, get an attribute's value

我的未来我决定 提交于 2019-12-17 21:29:30

问题


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>

回答1:


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




回答2:


 @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);
}



回答3:


Try attributes.getValue(i) method



来源:https://stackoverflow.com/questions/10242561/with-sax-parser-get-an-attributes-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!