程序提示错误,各位帮看一下
程序代码:
package 操作XML数据; import import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class Test { public static void main(String[] args) { //1、得到DOM解析器的工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //2、从DOM工厂获得DOM解析器 try { DocumentBuilder db = dbf.newDocumentBuilder(); //3、解析xml文档,得到一个Document,即DOM树 Document doc = db.parse("src/收藏信息.xml"); //4、得到所有<Brand>节点列表信息 NodeList list = doc.getElementsByTagName("Brand"); //5、循环Brand信息 for(int i=0;i<list.getLength();i++){ Node brandnode = list.item(i); Element brandElement = (Element)brandnode; String brandname = brandElement.getAttribute("name"); // System.out.println(brandname); NodeList childList = brandnode.getChildNodes(); for(int j = 0;j<childList.getLength();j++){ Element typeElement =(Element)childList.item(j); String typename = typeElement.getAttribute("name"); System.out.println("手机"+brandname+typename); } } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }运行的时候总是提示Exception in thread "main" java.lang.ClassCastException: com. cannot be cast to org.w3c.dom.Element
at 操作XML数据.Test.main(Test.java:35)
第三十五行是Element typeElement =(Element)childList.item(j);类型转换没错吧?怎么会报错呢?求科普