gpt4 book ai didi

java - XML 解析器给出 null 元素

转载 作者:行者123 更新时间:2023-12-01 05:51:21 28 4
gpt4 key购买 nike

当我尝试解析 XML 文件时,它有时会在标题处给出一个 null 元素。我认为这与 HTML 标签 '

有关

如何解决这个问题?

我有以下 XML 文件:

<item>
<title>&#039; Nieuwe DVD &#039;</title>
<description>tekst, tekst tekst</description>
<link>dvd.html</link>
<category>nieuws</category>
<pubDate>Sat, 1 Jan 2011 9:24:00 +0000</pubDate>
</item>

下面的代码用于解析 xml 文件:

//DocumentBuilderFactory, DocumentBuilder are used for 
//xml parsing
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

//using db (Document Builder) parse xml data and assign
//it to Element
Document document = db.parse(is);
Element element = document.getDocumentElement();

//take rss nodes to NodeList
element.normalize();

NodeList nodeList = element.getElementsByTagName("item");

if (nodeList.getLength() > 0)
{
for (int i = 0; i < nodeList.getLength(); i++)
{
//take each entry (corresponds to <item></item> tags in
//xml data

Element entry = (Element) nodeList.item(i);
entry.normalize();
Element _titleE = (Element) entry.getElementsByTagName(
"title").item(0);

Element _categoryE = (Element) entry
.getElementsByTagName("category").item(0);
Element _pubDateE = (Element) entry
.getElementsByTagName("pubDate").item(0);
Element _linkE = (Element) entry.getElementsByTagName(
"link").item(0);

String _title = _titleE.getFirstChild().getNodeValue();
String _category = _categoryE.getFirstChild().getNodeValue();
Date _pubDate = new Date(_pubDateE.getFirstChild().getNodeValue());
String _link = _linkE.getFirstChild().getNodeValue();

//create RssItemObject and add it to the ArrayList
RssItem rssItem = new RssItem(_title, _category, _pubDate, _link);

rssItems.add(rssItem);
conn.disconnect();
}

最佳答案

当您真正想要getTextContent时,不要使用getFirstElement

关于java - XML 解析器给出 null 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4575918/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com