2012-04-20 43 views
15

我使用Android從Web解析XML。下面的代碼顯示了一個XML示例。我遇到的問題是我無法獲取item標籤的字符串值。當我使用name = attributes.getQName(i);時,它輸出名稱,而不是屬性的值。使用代替使用SAX解析器,獲取屬性值

<weatherdata> 
<timetags> 
    <item name="date"> 
    <value>20/04/2012</value> 
    <unit/> 
    <image/> 
    <class>dynamic</class> 
    <description>The current date</description> 
    </item> 

回答

17

attributes.getValue(i); 

attributes.getQName(i); 

因爲doc說:

getQName返回屬性的限定(前綴)名。

的getValue通過限定(加前綴的)名稱查找屬性的值。

看到this例如用於獲取屬性名稱和值

2

嘗試attributes.getValue(i)方法

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