我对 Java 很陌生,所以我希望这是一个简单的问题。
我正在尝试读入并解析一个 XML 文件。我遵循了一些教程,所有教程都显示了这一行(变量名称不同,但概念相同):
Element eElement = (Element) nNode;
一旦我到达这条线并运行它,我的应用程序就会崩溃。我相信它应该采用节点项并将其转换为元素:
System.out.println("Root element :" + myDoc.getDocumentElement().getNodeName());
NodeList nList = myDoc.getElementsByTagName("title");
System.out.println("----------------------");
for(int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if(nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
}
}
日志猫:
05-03 02:12:20.865: I/System.out(9424): Root element :rss
05-03 02:12:20.865: I/System.out(9424): ----------------------
05-03 02:12:20.875: W/System.err(9424): java.lang.ClassCastException: org.apache.harmony.xml.dom.ElementImpl
05-03 02:12:20.875: W/System.err(9424): at com.sltrib.test.Main.onCreate(Main.java:68)
05-03 02:12:20.875: W/System.err(9424): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-03 02:12:20.875: W/System.err(9424): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-03 02:12:20.875: W/System.err(9424): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-03 02:12:20.875: W/System.err(9424): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-03 02:12:20.875: W/System.err(9424): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-03 02:12:20.875: W/System.err(9424): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 02:12:20.875: W/System.err(9424): at android.os.Looper.loop(Looper.java:123)
05-03 02:12:20.875: W/System.err(9424): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-03 02:12:20.875: W/System.err(9424): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 02:12:20.875: W/System.err(9424): at java.lang.reflect.Method.invoke(Method.java:507)
05-03 02:12:20.875: W/System.err(9424): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-03 02:12:20.875: W/System.err(9424): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-03 02:12:20.875: W/System.err(9424): at dalvik.system.NativeStart.main(Native Method)