首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

读取XML文件时,若TAG为空,则卡死,

2012-02-04 
读取XML文件时,若TAG为空,则卡死,求助!Eclipse 3.2JDK1.4.9想从XML文件中读取数据如果TAG非空,比如TITLE

读取XML文件时,若TAG为空,则卡死,求助!
Eclipse 3.2 JDK1.4.9

想从XML文件中读取数据
如果TAG非空,比如<TITLE>title</TITLE>可以正常读取;
但若TAG是空,如<TITLE></TITLE>则运行时会卡住(不往下执行,不报错不抛出异常)
我的代码大概如下:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(fileName);
doc.normalize();

String newsTitle = news.getElementsByTagName("TITLE").item(0).getFirstChild().getNodeValue();

请问是哪里的问题呢,谢谢!

[解决办法]
问题在那说不上来,但是还有一中取值方法!
1,String newsTitle = news.getElementsByTagName("TITLE").item(0).getFirstChild().getNodeValue(); 
2,String newsTitle = news.getElementsByTagName("TITLE").item(0).getTextContent();
建议用第二种方法取值!

热点排行