请问我这段java程序为什么会报错?报java.lang.ClassCastException
importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactor...
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Test {
public static void main(String[] args) {
try{
//创建解析器工厂
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//获得解析器
DocumentBuilder db= dbf.newDocumentBuilder();
//对XMl文档进行解析,得到Document
Document doc = db.parse("src/收藏信息.xml");
//得到所有《brand》列表信息
NodeList list = doc.getElementsByTagName("brand");
//循环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(typename);
System.out.println("手机:"+brandname+typename);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
} 展开
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Test {
public static void main(String[] args) {
try{
//创建解析器工厂
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//获得解析器
DocumentBuilder db= dbf.newDocumentBuilder();
//对XMl文档进行解析,得到Document
Document doc = db.parse("src/收藏信息.xml");
//得到所有《brand》列表信息
NodeList list = doc.getElementsByTagName("brand");
//循环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(typename);
System.out.println("手机:"+brandname+typename);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询