我想通过 XML 文件填充下拉列表。我已经创建了 XML 文件,并且我首先编写的代码是为了读取 xml 文件,只是给我 xml 文件中的项目正在编译,但是当我想稍后运行代码时给我错误。
public ArrayList readXML(){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
Document dom;
dom = db.parse("PVS_XML.xml");
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("item");
System.out.println(((Node) nl).getNodeValue());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
错误信息:
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl cannot be cast to org.w3c.dom.Node
at de.sidion.pvsng.pages.InputPage.readXML(InputPage.java:222)
at de.sidion.pvsng.pages.InputPage.init(InputPage.java:255)
at de.sidion.pvsng.pages.InputPage.<init>(InputPage.java:183)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)