From: Kevin on
Hello,

My code (shown below) tries to access the value of an XML element.
After I declare the variable "name" equal to "dog," I then create a
new variable, a, that gets all elements named "dog." Next, my
if...else statement is true for the if statement with this output:

"found name dog with length = 1"

Next, I try to access the first child node of all elements named
"dog." On this line my code fails with an
"xmlDoc.getElementsByTagName(name).childNodes[0]; not defined" error.

var name = "dog";

var a = xmlDoc.getElementsByTagName(name);

if(a) {alert("found name " + name + "with length = " + a.length);}
else {alert(QuoteName + " not found");}

var b = xmlDoc.getElementsByTagName(name).childNodes[0]; //
6-27-2010: undefined error

Please let me know your thoughts.

Thank you,
Kevin
From: Martin Honnen on
Kevin wrote:

> var b = xmlDoc.getElementsByTagName(name).childNodes[0]; //
> 6-27-2010: undefined error

getElementsByTagName gives you a NodeList, you can then access items in
the NodeList with the item method e.g.
var el0 = b.item(0);
or with the ECMAScript short cut of bracket notation
var el0 = b[0];
then you should be able to access childNodes of such an item in the
NodeList. Accessing the childNodes of the NodeList itself does is not
something the DOM allows, if you want such features look into XPath or
XQuery there you can do e.g.
//foo/child::node()
to access all child nodes of all foo elements.


--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
From: Kevin on
Martin, thanks for your help.

Here's what I did -

var a = xmlDoc.getElementsByTagName(name);


if(a) {alert("found name " + name + "with length = " +
a.length);}
else {alert(QuoteName + " not found");}

var b = a.item(0);
alert("b has length = " + b.length + " and value = " +
b.nodeValue);

// output: "b has length = undefined and value = null

Can you please tell me why I'm getting length = undefined and value =
null?

Thanks,
Kevin
From: RobG on
On Jul 8, 4:58 am, Kevin <kevin.m.mered...(a)gmail.com> wrote:
> Martin, thanks for your help.
>
> Here's what I did -
>
>        var a = xmlDoc.getElementsByTagName(name);

The getElementsByTagname method returns a NodeList, so a is now a
NodeList.

<URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-A6C9094 >


>
>         if(a)   {alert("found name " + name + "with length = " +
> a.length);}
>         else {alert(QuoteName + " not found");}
>
>         var b = a.item(0);

Variable b is assigned a reference to the first member of a, which
should be an ELement (given that a.length > 0), which implements the
Element interface which builds on the Node interface.

Element:
<URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-745549614 >

Node:
<URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247 >


>         alert("b has length = " + b.length + " and value = " +
> b.nodeValue);
>
> // output: "b has length = undefined and value = null
>
> Can you please tell me why I'm getting length = undefined and value =
> null?

Because b is an Element and neither interface Element nor Node define
a length property and you haven't assigned a value to one.

I think what you might be trying to do is discover how many child
nodes b has. If that is the case, then:

b.childNodes.length

should do the trick.


--
Rob
From: Garrett Smith on
On 2010-07-07 04:20 PM, RobG wrote:
> On Jul 8, 4:58 am, Kevin<kevin.m.mered...(a)gmail.com> wrote:
>> Martin, thanks for your help.
>>

[snip links, etc]

> Because b is an Element and neither interface Element nor Node define
> a length property and you haven't assigned a value to one.
>

Right, don't expect a length property. Also don't expect to not have a a
length property. Some elements actually do have a length property (such
as FORM, SELECT, ...).

--
Garrett