473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1442
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Zhang Weiwu | last post by:
This is really very stange. I have a form like this: <form id="form_A"> xxxx </form> This form is on one page in a page of a web application, it is the only form on that page. on the end of...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
11
by: Peter Oliphant | last post by:
Say I have a line in my code something like the following (NOTE: Point is __value class System::Drawing::Point): Point point = new Point ; How do I now 'ask' 'point' how many elements it has...
8
by: ljlevend2 | last post by:
If a property's type is an array and the property is exposed in a PropertyGrid, then the property can be expanded so that the user can view and edit the array elements. But, I've noticed that if...
3
by: Newcomsas | last post by:
Hello, I'm trying to solve a problem with JS textbox array without success. I have two buttons in my page: PLUS and MINUS; at every click on PLUS a new textbox named 'dear' is generated. So, if...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
6
by: pinetaj | last post by:
Hello, I have a question of using 'property' on accessing elements of array. There is an array member in a class. I'd like to restrict accessing the elements of the array through property. And...
4
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a method that takes a char array of 10. I have a char array of 30. how do I make it send the first 10, then the next 10, then the final 10 ? I need help with my looping skills....
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.