【问题标题】:With SAX Parser, get an attribute's value使用 SAX Parser,获取属性的值
【发布时间】:2012-05-01 19:44:29
【问题描述】:
我正在使用 Android 从 Web 解析 XML。下面的代码显示了 XML 的示例。我遇到的问题是我无法获取项目标签的字符串值。当我使用name = attributes.getQName(i); 时,它会输出名称,而不是属性的值。
<weatherdata>
<timetags>
<item name="date">
<value>20/04/2012</value>
<unit/>
<image/>
<class>dynamic</class>
<description>The current date</description>
</item>
【问题讨论】:
标签:
java
android
xml
saxparser
xml-attribute
【解决方案1】:
使用
attributes.getValue(i);
而不是
attributes.getQName(i);
因为正如doc 所说:
getQName:返回一个属性的限定(前缀)名称。
getValue :通过限定(前缀)名称查找属性值。
查看this 获取属性名称和值的示例
【解决方案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】:
试试attributes.getValue(i)方法