By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,598 Members | 1,894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,598 IT Pros & Developers. It's quick & easy.

how to retrive the elements of array.

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.