我在使用 Android XMLPullParser 获取 XML 值中的特定值时遇到问题。这是包含我需要的信息的 XML 文件的 fragment ,
<parameters applicable-location="point1">
<temperature type="apparent" units="Fahrenheit" time-layout="k-p1h-n1-1">
<value>50</value>
</temperature>
<temperature type="dew point" units="Fahrenheit" time-layout="k-p1h-n1-1">
<value>33</value>
</temperature>
我是解析 XML 的新手,但我需要检索
<value>
其中:
<temperature type="apparent" units="Fahrenheit" time-layout="k-p1h-n1-1">
现在对我来说,我会做这样的事情:
if(xpp.getName().equalsIgnoreCase("temperature") {
xpp.next(); //To bring the parser to that value tag
if(xpp.getName().equalsIgnoreCase("value") {
weather_apparenttemp.setText(xpp.getName());
}
}
但它不起作用,我觉得我错过了或做错了什么:(有什么帮助吗?谢谢!
最佳答案
试试这个代码:
if(xpp.getName().equalsIgnoreCase("temperature") {
if (xpp.getAttributeValue(null, "type").equals("apparent")){
xpp.next(); //To bring the parser to that value tag
if(xpp.getName().equalsIgnoreCase("value") {
weather_apparenttemp.setText(xpp.getName());
}
}
这是为了检索标签名称 <value>
内输入的温度如表所示。如果要检索该值,请使用 xpp.next()
推进 xpp在第二个条件内并获取文本 xpp.getText()
.
关于java - Android XmlPullParser 麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20315674/