当使用构建我的雅虎小部件 getElementsByTagName 返回元素而不是节点时
我可以看到 itemNodes 是一个 DOM 节点列表,其中包含与标题数量相同的条目数。我想访问标题节点内的信息。我尝试使用 itemNodes.childNodes[0].nodeValue
我收到错误
发生错误: TypeError: itemNodes.childNodes has noproperties
将 itemNodes.item(i) 放入循环中返回
Title 1: [object DOMElement] 标题 2:[对象 DOMElement] 标题 3:[对象 DOMElement] 标题 4:[object DOMElement]
我期望 DOM 节点。我做错了什么?我在 Vista 机器上使用 Yahoo widgets 4.5? 我的 .KON 文件中有以下内容。
x = filesystem.readFile('sample.xml');
doc = XMLDOM.parse(x);
if(doc != null)
{
//print( doc.toXML() );
var itemNodes = doc.getElementsByTagName('title');
var firstItem = itemNodes.item(0);
print(itemNodes);
numberOfItems = itemNodes.length;
items=null;
items = new Array(numberOfItems);
for(var i = 0; i < numberOfItems; i++)
{
print("Title " + (i+1) + ": " + itemNodes );
}
}
else
{
print("An error occurred. Response status: (" + request.status + ") " + request.statusText);
}
}
catch(e)
{
print("An error occurred: " + e);
}
样本.xml如下
<!-- Edited by XMLSpy® -->
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
I can see that itemNodes is a DOM nodelist containing the same number of entrys as number of titles. I want to access the information inside the title node. I have tried using itemNodes.childNodes[0].nodeValue
I receive the error
An error occurred: TypeError: itemNodes.childNodes has no properties
placing itemNodes.item(i) inside the loop returns
Title 1: [object DOMElement]
Title 2: [object DOMElement]
Title 3: [object DOMElement]
Title 4: [object DOMElement]
I expected DOM nodes. what have I done wrong? I'm using Yahoo widgets 4.5 on a vista machine?
I have the following in my .KON file.
x = filesystem.readFile('sample.xml');
doc = XMLDOM.parse(x);
if(doc != null)
{
//print( doc.toXML() );
var itemNodes = doc.getElementsByTagName('title');
var firstItem = itemNodes.item(0);
print(itemNodes);
numberOfItems = itemNodes.length;
items=null;
items = new Array(numberOfItems);
for(var i = 0; i < numberOfItems; i++)
{
print("Title " + (i+1) + ": " + itemNodes );
}
}
else
{
print("An error occurred. Response status: (" + request.status + ") " + request.statusText);
}
}
catch(e)
{
print("An error occurred: " + e);
}
The sample.xml is as follows
<!-- Edited by XMLSpy® -->
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用以下内容访问了 XML 的内容。 TrimString 是一个自定义函数。我认为小部件调试器将 DomNodes 显示为 DomElements
I accessed the contents of the XML using the following. trimString is a custom function. I think the widget debugger showed DomNodes as DomElements
Yahoo Widget 引擎在 XML 解析方面的工作方式有所不同。
如果您用于
访问第一个子节点的值,则将表达式更改为,
通常您可以使用以下表达式访问第 i 个子节点的值,
Yahoo Widget engine works differently when it comes to XML parsing.
if you using
to access value of first child node then change your expression to,
In general you can access value of ith child using below expression,