当使用构建我的雅虎小部件 getElementsByTagName 返回元素而不是节点时

发布于 2024-08-10 02:51:48 字数 2143 浏览 4 评论 0原文

我可以看到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错爱 2024-08-17 02:51:48

我使用以下内容访问了 XML 的内容。 TrimString 是一个自定义函数。我认为小部件调试器将 DomNodes 显示为 DomElements

for(var i = 0; i < numberOfItems; i++)
    {

            //get each item node
            var node = itemNodes.item(i);
            var titleList = node.evaluate( "title/text()" );
            var titleString =  trimString(String(titleList.item(0).nodeValue));
            print("TitleX: " + titleString);
    }

I accessed the contents of the XML using the following. trimString is a custom function. I think the widget debugger showed DomNodes as DomElements

for(var i = 0; i < numberOfItems; i++)
    {

            //get each item node
            var node = itemNodes.item(i);
            var titleList = node.evaluate( "title/text()" );
            var titleString =  trimString(String(titleList.item(0).nodeValue));
            print("TitleX: " + titleString);
    }
糖果控 2024-08-17 02:51:48

Yahoo Widget 引擎在 XML 解析方面的工作方式有所不同。
如果您用于

itemNodes.childNodes[0].nodeValue

访问第一个子节点的值,则将表达式更改为,

itemNodes.childNodes.item(0).nodeValue

通常您可以使用以下表达式访问第 i 个子节点的值,

itemNodes.childNodes.item(i).nodeValue

Yahoo Widget engine works differently when it comes to XML parsing.
if you using

itemNodes.childNodes[0].nodeValue

to access value of first child node then change your expression to,

itemNodes.childNodes.item(0).nodeValue

In general you can access value of ith child using below expression,

itemNodes.childNodes.item(i).nodeValue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文