试图创建一个Android应用程序,但SAXparser将无法识别并显示文档中的tempertaure标记数据,并设置错误并且我已经尝试了所有可以解决的问题
代码xml文件
<weatherdata>
<timetags>
<item name="date">
<value>26/07/2011</value>
<unit/>
<image/>
<class>dynamic</class>
<description>The current date</description>
</item>
</timetags>
<temperaturetags>
<item name="temp">
<value>21.8</value>
</temperaturetags>
</weatherdata>
XML处理程序
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLhandler extends DefaultHandler {
Boolean currentElement = false;
Boolean temperature = false;
String currentValue = null;
public static weatherlist sitesList = null;
public static weatherlist getSitesList() {
return sitesList;
}
public static void setSitesList(weatherlist sitesList) {
XMLhandler.sitesList = sitesList;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("weatherdata"))
{
currentElement = true;
/** Start*/
sitesList = new weatherlist();
} if (localName.equals("temperaturetags")) {
temperature = true;
/** Start
sitesList = new weatherlist();*/
} else if (localName.equals("item")) {
/** Get attribute value */
String attr = attributes.getValue("name");
sitesList.setValue(attr);
}
}
/** Called when tag closing*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
if (localName.equals("weatherdata"))
{
// currentElement = false;
} else if (localName.equalsIgnoreCase("temperaturetags")) {
this.temperature = false;
sitesList.settemperature(currentValue);
} else if (localName.equalsIgnoreCase("item")) {
sitesList.setName(currentValue);
} else if (localName.equalsIgnoreCase("value")){
sitesList.setitem(currentValue);}
}
// currentElement = false;
/** set value
if (localName.equalsIgnoreCase("item"))
sitesList.setName(currentValue);
else if (localName.equalsIgnoreCase("value"))
sitesList.setitem(currentValue);
}*/
/** Called to get tag characters */
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
代码:arraylist
import java.util.ArrayList;
public class weatherlist {
private ArrayList<String> temperature = new ArrayList<String>();
private ArrayList<String> value = new ArrayList<String>();
private ArrayList<String> name = new ArrayList<String>();
private ArrayList<String> item = new ArrayList<String>();
/** In Setter method default it will return arraylist
* change that to add */
public ArrayList<String> gettemperature() {
return temperature;
}
public void settemperature(String temperature) {
this.temperature.add(temperature);
}
public ArrayList<String> getvalue() {
return value;
}
public void setValue(String value) {
this.value.add(value);
}
public ArrayList<String> getName() {
return name;
}
public void setName(String name) {
this.name.add(name);
}
public ArrayList<String> getitem() {
return item;
}
public void setitem(String item) {
this.item.add(item);
}
}
有人请帮忙! 提前谢谢
答案 0 :(得分:0)
尝试关闭商品标记
<temperaturetags>
<item name="temp">
<value>21.8</value>
</item>
</temperaturetags>