我一直在尝试通过获取 item
节点的长度来迭代子节点。但是,它显示的长度是原始长度的两倍。
这是我一直在使用的 XML 代码,
<item>
<title>Title 1</title>
<link>http://www.example.com</link>
<comments>http://www.example.com/#comments</comments>
<pubDate>Mon, 19 Jan 2015 04:05:39 +0000</pubDate>
<dc:creator><![CDATA[Exampple]]></dc:creator>
<category><![CDATA[Hot News]]></category>
<guid isPermaLink="false">http://www.example.com/?p=72278</guid>
<description><![CDATA[<p>Some Description</p>]]></description>
<content:encoded><![CDATA[<p>Some description content.</p>]]></content:encoded>
<wfw:commentRss>http://www.example.com/feed/</wfw:commentRss>
</item>
Java 代码:
NodeList nl = doc.getElementsByTagName("item");
int length = nl.getLength();
Log.d("check", "Item i count = " + length); //shows 10
for (int i = 0; i < length; i++) {
Node currentNode = nl.item(i); // Get the first item
String nodetestName = currentNode.getNodeName();
NodeList nchild = currentNode.getChildNodes(); // Get the child nodes of first item
int clength = nchild.getLength(); // Get the length of child nodes of first item (Shows 21 but original length is 10)
我应该做什么?
最佳答案
尝试忽略空格,它对我有用。
docBuilderFactory.setIgnoringElementContentWhitespace(true);
关于java - 访问子节点的长度返回原始计数的两倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28018163/