getChildNodes 使用出现的逻辑错误

前端之家收集整理的这篇文章主要介绍了getChildNodes 使用出现的逻辑错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<span style="font-size:18px;">Document doc = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance() ;
		// 取得DocumentBuilder类的对象
		DocumentBuilder build = null;
		try {
			build = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			doc = build.parse(new File("e:/output.xml")) ;
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	// 创建一个新的XML文档;
		NodeList list = doc.getChildNodes();
		list = list.item(0).getChildNodes();
		for(int i=0;i<list.getLength();i++)
		System.out.println(list.item(i).getNodeName());</span>

最近在封装dom 读取xml文件时遇到的问题

<span style="font-size:18px;"><?xml version="1.0" encoding="GBK" standalone="no"?>
<addresslist><linkman><name>李兴华</name><email>mldnqa@163.com</email><ceshi>1223</ceshi></linkman></addresslist></span>

如果在editplus中把上面的xml 代码整齐排放 如下 就会在读取的时候出现错误 因为这时候解析会把xml里面空格、tab、回车都有可能当成node。所以api里面专门有个方法:ignorableWhitespace 是用来处理这个问题的

在使用时最好使用document 里面的 getElementsByTagName(tagname)

<span style="font-size:18px;"><?xml version="1.0" encoding="GBK" standalone="no"?>
<addresslist>
	<linkman>
		<name>李兴华</name>
		<email>mldnqa@163.com</email>
		<ceshi>1223</ceshi>
	</linkman>
</addresslist></span>
原文链接:https://www.f2er.com/xml/297501.html

猜你在找的XML相关文章