470,580 Members | 2,154 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,580 developers. It's quick & easy.

how to retrive the elements of array.

Hi

I'm a JavaSCript newbee and i've got a problem.

I do a search and get the following in xml:

<user>
<exec>
<entry>Value1</entry>
<entry>Value2</entry>
<entry>Value3</entry>
<entry>Value4</entry>
<exec/>
<user/>

How is the easiest way to find the number of entries in the array and
how can iterate over each value to test if the given value matches
another variable?

Thanks
JT
Jul 20 '05 #1
3 1395
Hello,

This is not the optimal solution:

<script>

var s=
'<user>'+
' <exec>'+
' <entry>Value1</entry>'+
' <entry>Value2</entry>'+
' <entry>Value3</entry>'+
' <entry>Value4</entry>'+
' <exec/>'+
'<user/>';

var i, j, entry;
while(true)
{
i = s.indexOf('<entry>');
if (i == -1)
break;
s = s.substr(i+7); // chop off <entry>
j = s.indexOf('</entry>');
entry = s.substr(0, j);
alert(entry);
}
</script>
--
Elias
http://lgwm.org/
"sunsmurf" <jo***@yahoo.com> wrote in message
news:89**************************@posting.google.c om...
Hi

I'm a JavaSCript newbee and i've got a problem.

I do a search and get the following in xml:

<user>
<exec>
<entry>Value1</entry>
<entry>Value2</entry>
<entry>Value3</entry>
<entry>Value4</entry>
<exec/>
<user/>

How is the easiest way to find the number of entries in the array and
how can iterate over each value to test if the given value matches
another variable?

Thanks
JT

Jul 20 '05 #2
sunsmurf wrote:
I do a search and get the following in xml:

<user>
<exec>
<entry>Value1</entry>
<entry>Value2</entry>
<entry>Value3</entry>
<entry>Value4</entry>
<exec/>
<user/>

How is the easiest way to find the number of entries in the array
Get the `exec' element an ID, then access it using

document.getElementById("execElementID")

Otherwise you need to access the element using the collection

document.getElementsByTagName("exec")

returns.
and how can iterate over each value to test if the given value
matches another variable?


Provided the element has the ID `execElementID':

var o = document.getElementById("execElementID");

Or, if it is the first/only `exec' element:

var o = document.getElementsByTagName("exec")[0];

You can always use:

if (o)
{
for (var i = 0; i < o.childNodes.length; i++)
{
if (o.childNodes[i].nodeValue == foobar)
// do something
}
}

Untested, read http://www.w3.org/TR/DOM-Level-2-Core/ for details.
PointedEars
--
apprentice.c - parses /etc/magic to learn magic

(from the file-3.40 README)
Jul 20 '05 #3
On 8 Dec 2003 08:14:14 -0800, jo***@yahoo.com (sunsmurf) wrote:
Hi

I'm a JavaSCript newbee and i've got a problem.

I do a search and get the following in xml:

<user>
<exec>
<entry>Value1</entry>
<entry>Value2</entry>
<entry>Value3</entry>
<entry>Value4</entry>
<exec/>
<user/>

How is the easiest way to find the number of entries in the array and
how can iterate over each value to test if the given value matches
another variable?


If you only care about MSXML you can do both at the same time.

var lookupValue = "Value1";
var dom = new ActiveXObject("MSXML2.DomDocument.4.0");
dom.async = false;
dom.loadXML(<xml string>);
var nodes = dom.selectNodes('/user/exec/entry[. = '" + lookupValue +
"']);
alert(nodes.length);
alert(nodes.item(0).text);

Regards,
Steve
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Peter Oliphant | last post: by
24 posts views Thread by RyanTaylor | last post: by
4 posts views Thread by =?Utf-8?B?cm9nZXJfMjc=?= | last post: by
19 posts views Thread by arnuld | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.