确定这个节点集的长度?
Mozilla 的 Javascript:
while (result)
{
alert(result.childNodes[0].nodeValue);
//txt=txt+result.childNodes[0].nodeValue+"<br/>";
result=nodes.iterateNext();
}
旨在从 xml 文件返回一系列字符串。当我使用警报行时,它会按预期使用一系列正确的字符串发出警报。注释行上的“txt”变量应该转到函数中更下方的innerHTML。但 Firebug 一直告诉我 result.childNodes[0] 未定义。为什么它会在警报中定义而不是在下一行中定义?
我希望有足够的代码来确定问题......如果没有,我会发布更多内容。
感谢帮助
[编辑]
这是结果的定义:
var x=xmlDoc.responseXML;
var nodes=x.evaluate(path, x, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();
我正在检索 XML
[编辑]
好吧,我将迭代器放入这样的 for 循环中:
for (var i=0; i<2; i++)
{
var res=(xmlNodes.childNodes[0].nodeValue);
txt=txt+res+"<br/>";
xmlNodes=xmlQuery.iterateNext();
}
因为我有一个理论,一旦迭代为空,循环就会失败。这就是发生的事情 - 它一直有效,直到我将循环一个实例设置为高于可用节点的数量。现在我想弄清楚如何获取节点集的“长度”。我该怎么做?
Javascript for Mozilla:
while (result)
{
alert(result.childNodes[0].nodeValue);
//txt=txt+result.childNodes[0].nodeValue+"<br/>";
result=nodes.iterateNext();
}
This is intended to return a series of strings from an xml file. When I use the alert line, it alerts as expected with the proper strings in a series. The "txt" variable on the commented line is supposed to go to an innerHTML further down in the function. But Firebug keeps telling me that the result.childNodes[0] is undefined. Why would it be defined in the alert but not the next line?
I hope that's enough code to determine the problem...if not I will post more.
Thanks for help
[edit]
this is the definition of result:
var x=xmlDoc.responseXML;
var nodes=x.evaluate(path, x, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();
I am retrieving XML
[edit]
okay I put the iterator in a for loop like this:
for (var i=0; i<2; i++)
{
var res=(xmlNodes.childNodes[0].nodeValue);
txt=txt+res+"<br/>";
xmlNodes=xmlQuery.iterateNext();
}
because I had a theory that once the iteration was null the loop would fail. So that's what happened--it worked until I set the loop one instance higher than the amount of nodes available. now I am trying to figure out how to get the "length" of the node-set. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
到目前为止,我在您显示的代码中没有看到任何问题。如果
childNodes[0]
未定义,则它必须是文本节点或空节点,并且在尝试访问诸如nodeValue
之类的属性时,您应该会看到异常未定义的childNodes[0]
。异常将显示在警报
或串联或任何其他类型的访问中。这是您更新的问题的答案。
您可以从评估函数返回以下节点集类型:
我将跳过“第一个节点”,因为这不适用于这种情况。
迭代器
使用迭代器,您只能获得一个用于顺序遍历节点的
iterateNext()
方法。迭代器引用活动节点,这意味着如果在遍历结果集时文档中的节点发生更改,迭代器将变得无效。下面是一个使用迭代器遍历每个结果节点的示例:
如果您想使用迭代器并查找匹配结果的数量,则迭代所有节点并在数组中返回它们的包装函数可能会很有用。
以数组形式获取节点并循环该数组:
快照
快照提供查询时节点的静态结果。对文档的更改不会影响此快照。这里有用的接口是
snapshotLength
属性和snapshotItem(index)
函数。以下是使用快照结果的示例:
请参阅工作示例。
看来你正在为 Firefox 开发这个。您是否考虑过使用 E4X 来实现此目的?它提供了一个非常简单的界面来处理 XML 文档 - 用于创建、操作和查询。
I don't see any problems in the code you have shown so far. If
childNodes[0]
is undefined, then it has to be a text node or an empty node, and you should see an exception when trying to access a property such asnodeValue
ofchildNodes[0]
which is undefined. The exception will show up onalert
or concatenation, or any other type of access.This is the answer to your updated question.
You can have the following types of node-sets returned from the evaluate function:
I'll skip "first nodes" as that doesn't apply in this situation.
Iterators
With iterators, you only get an
iterateNext()
method for traversing nodes sequentially. Iterators refer to live nodes, meaning if the nodes in the document were to change while you are traversing the resultset, the iterator will become invalid.Here's an example with using an iterator to go over each resulting node:
If you want to use iterators, and find the number of matching results, a wrapper function that iterates through all nodes and returns them in an array might be useful.
Get nodes as an array and loop the array:
Snapshots
Snapshots provide a static result of the nodes at the time of querying. Changes to the document will not affect this snapshot. Useful interfaces here will be the
snapshotLength
property, andsnapshotItem(index)
function.Here's an example using a snapshot result:
See a working example.
It seems you are developing this for Firefox. Did you consider using E4X for this purpose? It provides a really easy interface for dealing with XML documents - for creating, manipulating, and querying.
在 XPath 中,这是使用标准 XPath
count()< 来实现的/code>
函数。
count(someExpression)
计算结果为
someExpression
选择的节点数。In XPath this is achieved using the standard XPath
count()
function.count(someExpression)
evaluates to the number of nodes selected by
someExpression
.