how to retrive the elements of array.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sunsmurf

    #1

    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
  • lallous

    #2
    Re: how to retrive the elements of array.

    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('<ent ry>');
    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

    "sunsmurf" <jot12@yahoo.co m> wrote in message
    news:893a48c3.0 312080814.2585e 644@posting.goo gle.com...[color=blue]
    > 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[/color]


    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: how to retrive the elements of array.

      sunsmurf wrote:
      [color=blue]
      > 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[/color]

      Get the `exec' element an ID, then access it using

      document.getEle mentById("execE lementID")

      Otherwise you need to access the element using the collection

      document.getEle mentsByTagName( "exec")

      returns.
      [color=blue]
      > and how can iterate over each value to test if the given value
      > matches another variable?[/color]

      Provided the element has the ID `execElementID' :

      var o = document.getEle mentById("execE lementID");

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

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

      You can always use:

      if (o)
      {
      for (var i = 0; i < o.childNodes.le ngth; 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)

      Comment

      • Steve van Dongen

        #4
        Re: how to retrive the elements of array.

        On 8 Dec 2003 08:14:14 -0800, jot12@yahoo.com (sunsmurf) wrote:
        [color=blue]
        >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?[/color]

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

        var lookupValue = "Value1";
        var dom = new ActiveXObject(" MSXML2.DomDocum ent.4.0");
        dom.async = false;
        dom.loadXML(<xm l string>);
        var nodes = dom.selectNodes ('/user/exec/entry[. = '" + lookupValue +
        "']);
        alert(nodes.len gth);
        alert(nodes.ite m(0).text);

        Regards,
        Steve

        Comment

        Working...