XML解析,出现ClassCastException 原因
来源:互联网 发布:wol网络唤醒 编辑:程序博客网 时间:2024/06/06 16:30
import java.io.IOException;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 sddddddddddddd { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { // TODO Auto-generated method stub DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //从DOM工厂获得解析器 DocumentBuilder db = dbf.newDocumentBuilder(); //获得DOM树,解析出来文档 Document doc=db.parse("D:\\Phone.xml"); //获得节点信息 System.out.println("hh"); NodeList brandlist = doc.getElementsByTagName("Brand"); for (int i=0;i<brandlist.getLength();i++){ System.out.println("hh"); Node brand = brandlist.item(i); Element element = (Element) brand; String attrValue = element.getAttribute("name"); NodeList types = element.getChildNodes(); System.out.println("hh"); for (int j=0;j<types.getLength();j++){ Element typelement = (Element) types.item(j); String type = typelement.getAttribute("name"); System.out.println(attrValue+type); System.out.println("hh"); } } }}
原因在于,XML中间不能有空格,在网络上也有很多人指出了这个问题。
<?xml version="1.0" encoding="utf-8" ?><phone><Brand name="华为"><Type name="mate7"/><Type name="P7"/><Type name="荣耀7"/></Brand><Brand name="苹果"><Type name="Iphone5"/><Type name="Iphone6"/><Type name="Iphone7"/></Brand></phone>
如果有空格或者回车存在,就会导致错误的解析,API认为也是一个节点,结果解析出来什么都没有,就是null
0 0
- XML解析,出现ClassCastException 原因
- Gson 解析泛型出现ClassCastException
- weblogic10出现ClassCastException: weblogic.xml.jaxp.RegistrySAXParserFactory cannot be cast
- android removeFooterView classCastException 原因
- SDN之出现原因解析
- List.toArray()出现ClassCastException
- ClassCastException 错误解析
- ClassCastException 错误解析
- ClassCastException 错误解析
- xml解析出现的错误
- 编写程序时候出现了问题ClassCastException: interface javax.xml.soap.Text
- weblogic使用了自己带的xml解析器造成的ClassCastException
- SharedPreferences.getInt() 出现ClassCastException错误
- XML布局引起的ClassCastException
- java.lang.ClassCastException: 错误解析
- SAX解析xml出现的问题
- sax解析xml运行出现错误
- Spring4.xx出现无法解析xml文件
- 设计模式六大原则
- Android 个层次间调用关系
- 杭电ACM 2021:发工资咯
- 程序员面试宝典(33)-在O(1)时间删除链表结点
- Android Studio-1.2版本设置教程
- XML解析,出现ClassCastException 原因
- LayoutInflater详解
- hdu2457AC自动机+DP
- Git SSH Key生成步骤
- ********随便看看**********
- PyQt中QLabel背景与字体的一些设置
- 梯度下降 VS 随机梯度下降
- java并发编程实战手册第三章同步辅助类Phaser
- Mysql常识