【发布时间】:2013-11-22 14:31:21
【问题描述】:
所以我正在使用 xmlpullparser 解析 XML。我将它本地存储在android项目中,但问题是我想从url加载xml。下面的代码要改什么?
提前致谢
public String getItemFromXML(Activity activity) throws XmlPullParserException, IOException{
StringBuffer stringBuffer = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(R.xml.items);
xpp.next();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_TAG){
if (xpp.getName().equals("Prostorija")){
stringBuffer.append(xpp.getAttributeValue(null, "v") + "\n");
}
if (xpp.getName().equals("prozor")){
stringBuffer.append(xpp.getAttributeValue(null, "v") + "\n");
}
if (xpp.getName().equals("vrata")){
stringBuffer.append(xpp.getAttributeValue(null, "v") + "\n");
}
if (xpp.getName().equals("tempratura")){
stringBuffer.append(xpp.getAttributeValue(null, "v") + "\n");
}
}
eventType = xpp.next();
}
return stringBuffer.toString();}}
【问题讨论】: