我将尝试阅读xml,按照在线查找的教程http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/
public String leggi_palinsesto()
{
String comodo="";
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
//Element e = null;
Element e = (Element) nl.item(i);
String titolo = parser.getValue(e, KEY_TITOLO); // name child value
String artista = parser.getValue(e, KEY_ARTISTA); // cost child value
System.out.println(artista+" - "+titolo);
comodo=artista+" - "+titolo;
}
return comodo;
}
当调试到达Element e = (Element) nl.item(i);
时返回classcastexception错误,但我确定这是正确的代码。
这是Logcat
07-06 18:03:14.561: E/AndroidRuntime(1602): FATAL EXCEPTION: main
07-06 18:03:14.561: E/AndroidRuntime(1602): java.lang.ClassCastException: org.apache.harmony.xml.dom.ElementImpl
07-06 18:03:14.561: E/AndroidRuntime(1602): at it.axiomatic.radioamicizia.RadioAmiciziaActivity.leggi_palinsesto(RadioAmiciziaActivity.java:254)
07-06 18:03:14.561: E/AndroidRuntime(1602): at it.axiomatic.radioamicizia.RadioAmiciziaActivity$1$1.run(RadioAmiciziaActivity.java:121)
07-06 18:03:14.561: E/AndroidRuntime(1602): at android.os.Handler.handleCallback(Handler.java:587)
07-06 18:03:14.561: E/AndroidRuntime(1602): at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 18:03:14.561: E/AndroidRuntime(1602): at android.os.Looper.loop(Looper.java:123)
07-06 18:03:14.561: E/AndroidRuntime(1602): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-06 18:03:14.561: E/AndroidRuntime(1602): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 18:03:14.561: E/AndroidRuntime(1602): at java.lang.reflect.Method.invoke(Method.java:521)
07-06 18:03:14.561: E/AndroidRuntime(1602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-06 18:03:14.561: E/AndroidRuntime(1602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-06 18:03:14.561: E/AndroidRuntime(1602): at dalvik.system.NativeStart.main(Native Method)
这是.xml文件:
http://www.radioamicizia.com/demo.xml
编辑:我尝试过原始示例xml文件(http://api.androidhive.info/pizza/?format=xml),但在将节点转换为元素时...错误!
编辑:
static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITOLO = "name";
static final String KEY_ARTISTA = "cost";
有什么建议吗?
答案 0 :(得分:2)
来自Java documentation:
公共节点项(int index)
NodeList.item(i)
会返回Node
而不是Element
。该节点可以是其他任何东西,而不仅仅是Element
,这解释了为什么会出现这种错误。
如果您只想处理XML的元素,您应该在以下之前进行检查:
if(nl.item(i).getNodeType() == Node.ELEMENT_NODE){
Element e = (Element) nl.item(i);
// Process element here
}
编辑用这个替换你的for
循环:
for (int i = 0; i < nl.getLength(); i++) {
if(nl.item(i).getNodeType() != Node.ELEMENT_NODE)
continue;
Element e = (Element) nl.item(i);
String titolo = parser.getValue(e, KEY_TITOLO); // name child value
String artista = parser.getValue(e, KEY_ARTISTA); // cost child value
System.out.println(artista+" - "+titolo);
comodo=artista+" - "+titolo;
}