views:

48

answers:

2

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>
A: 

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);
    }
Dave
A: 

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

Ram