IC SunsetThe developerWorks Connections platform will be sunset on December 31, 2019. On January 1, 2020, this forum will no longer be available. More details available on our FAQ.
Topic
  • 7 replies
  • Latest Post - ‏2014-09-20T12:05:39Z by jitupkp
SystemAdmin
SystemAdmin
1140 Posts

Pinned topic newbie: cross-browser responseXML parsing

‏2007-09-29T02:46:24Z |
Hello
If I want to only get at the data in the responseXML is the only cross-browser way to use javascript:
var thisElementArray = responseXML.getElementsByTagName("thisElement"). This would generate as many js variables as elements. For example if the xml is like this
<collection>
<items>
<item>
<sku/><color/><size/>
</item>
<item>
<sku/><color/><size/>
</items>
</collection>
What I really want is a scriptable multi-dimensional array of sku,color,size. Is the only I can get that by
var sku = responseXML.getElementsByTagName("sku")
var color= responseXML.getElementsByTagName("color")
var size= responseXML.getElementsByTagName("size")

and have to work with the vectors independently, but conceptually like one. I would also have to do bounds checking on each. This all seems very cumbersome. Is there an easier or actually correct way, grin.
Updated on 2011-03-19T07:06:30Z at 2011-03-19T07:06:30Z by rupeshvbhavsar
  • SystemAdmin
    SystemAdmin
    1140 Posts

    Re: newbie: cross-browser responseXML parsing

    ‏2007-09-29T12:41:05Z  
    getElementsByTagName can be applied to any element in the tree to search from that element on down. So you should be doing this:

    
    <br /> var itemarray = [ ] ;<br /> var items = responseXML.getElementsByTagName( 
    "item" ) ;<br /> 
    
    for( var i = 0; i < items.length; i++ ) 
    {<br /> var sku = items<i>. getElementsByTagName( 
    "sku" ) ;<br /> var color = items<i>. getElementsByTagName( 
    "color" ) ;<br /> var size = items<i>. getElementsByTagName( 
    "size" ) ;<br /> itemarray.push( 
    { sku: sku.firstChild.value,<br /> color: color.firstChild.value, size: size.firstChild.value 
    } ) ;<br /> 
    }<br />
    


    I'm not sure about the firstChild syntax, but it's close to that.
  • SystemAdmin
    SystemAdmin
    1140 Posts

    Re: newbie: cross-browser responseXML parsing

    ‏2007-09-29T12:42:16Z  
    getElementsByTagName can be applied to any element in the tree to search from that element on down. So you should be doing this:

    <pre class="jive-pre"> <br /> var itemarray = [ ] ;<br /> var items = responseXML.getElementsByTagName( "item" ) ;<br /> for( var i = 0; i < items.length; i++ ) {<br /> var sku = items<i>. getElementsByTagName( "sku" ) ;<br /> var color = items<i>. getElementsByTagName( "color" ) ;<br /> var size = items<i>. getElementsByTagName( "size" ) ;<br /> itemarray.push( { sku: sku.firstChild.value,<br /> color: color.firstChild.value, size: size.firstChild.value } ) ;<br /> }<br /> </pre>

    I'm not sure about the firstChild syntax, but it's close to that.
    Sorry. It should be:

    
    <br /> var itemarray = [ ] ;<br /> var items = responseXML.getElementsByTagName( 
    "item" ) ;<br /> 
    
    for( var i = 0; i < items.length; i++ ) 
    {<br /> var sku = items<i>.getElementsByTagName( 
    "sku" ) ;<br /> var color = items<i>.getElementsByTagName( 
    "color" ) ;<br /> var size = items<i>.getElementsByTagName( 
    "size" ) ;<br /> itemarray.push( 
    { sku: sku.firstChild.value,<br /> color: color.firstChild.value, size: size.firstChild.value 
    } ) ;<br /> 
    }<br />
    
  • SystemAdmin
    SystemAdmin
    1140 Posts

    Re: newbie: cross-browser responseXML parsing

    ‏2007-09-29T12:43:28Z  
    Sorry. It should be:

    <pre class="jive-pre"> <br /> var itemarray = [ ] ;<br /> var items = responseXML.getElementsByTagName( "item" ) ;<br /> for( var i = 0; i < items.length; i++ ) {<br /> var sku = items<i>.getElementsByTagName( "sku" ) ;<br /> var color = items<i>.getElementsByTagName( "color" ) ;<br /> var size = items<i>.getElementsByTagName( "size" ) ;<br /> itemarray.push( { sku: sku.firstChild.value,<br /> color: color.firstChild.value, size: size.firstChild.value } ) ;<br /> }<br /> </pre>
    Wait. I see Developerworks is eating my array indices:

    
    <br /> var itemarray = [ ] ;<br /> var items = responseXML.getElementsByTagName( 
    "item" ) ;<br /> 
    
    for( var i = 0; i < items.length; i++ ) 
    {<br /> var sku = items [ i ] .getElementsByTagName( 
    "sku" ) ;<br /> var color = items [ i ] .getElementsByTagName( 
    "color" ) ;<br /> var size = items [ i ] .getElementsByTagName( 
    "size" ) ;<br /> itemarray.push( 
    { sku: sku.firstChild.value,<br /> color: color.firstChild.value, size: size.firstChild.value 
    } ) ;<br /> 
    }<br />
    
  • SystemAdmin
    SystemAdmin
    1140 Posts

    Re: newbie: cross-browser responseXML parsing

    ‏2007-09-29T15:54:35Z  
    Wait. I see Developerworks is eating my array indices:

    <pre class="jive-pre"> <br /> var itemarray = [ ] ;<br /> var items = responseXML.getElementsByTagName( "item" ) ;<br /> for( var i = 0; i < items.length; i++ ) {<br /> var sku = items [ i ] .getElementsByTagName( "sku" ) ;<br /> var color = items [ i ] .getElementsByTagName( "color" ) ;<br /> var size = items [ i ] .getElementsByTagName( "size" ) ;<br /> itemarray.push( { sku: sku.firstChild.value,<br /> color: color.firstChild.value, size: size.firstChild.value } ) ;<br /> }<br /> </pre>
    Hi jherr
    Thanks this is a little beyond my present "pay grade". Help understand and know where to learn more. I recognize that jason in the itemarray.push line. Is that dereferencing the nodevalue for each of the elements for each iteration and storing it as a text value in the indexed array location or do I still have to deal with a DOM segement? Can I then treat the itemarray as java/js style multi-dimensional array, get it row/column length? This has been very helpful this far. Thank you.
  • rupeshvbhavsar
    rupeshvbhavsar
    1 Post

    Re: newbie: cross-browser responseXML parsing

    ‏2011-03-19T07:06:30Z  
    Hi jherr
    Thanks this is a little beyond my present "pay grade". Help understand and know where to learn more. I recognize that jason in the itemarray.push line. Is that dereferencing the nodevalue for each of the elements for each iteration and storing it as a text value in the indexed array location or do I still have to deal with a DOM segement? Can I then treat the itemarray as java/js style multi-dimensional array, get it row/column length? This has been very helpful this far. Thank you.
    thik hai
  • jitupkp
    jitupkp
    104 Posts

    Re: newbie: cross-browser responseXML parsing

    ‏2014-09-20T12:05:17Z  

    see more

    http://techgurulab.com/course/ajax-quiz-online/

  • jitupkp
    jitupkp
    104 Posts

    Re: newbie: cross-browser responseXML parsing

    ‏2014-09-20T12:05:39Z  
    thik hai

    see more

    http://techgurulab.com/course/ajax-quiz-online/