[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

javascript array

http://www.w3schools.com/xml/xml_to_html.asp

From above example, I would like to return to array in javascript. How can I do that?
The ideal result will be like

Array("data1","data2")......

Thanks
0
ITsolutionWizard
Asked:
ITsolutionWizard
  • 2
  • 2
1 Solution
 
Ray PaseurCommented:
That's a very, very old example and you should not be using it.  We use jQuery for this sort of thing now-a-days and we use JSON instead of XML.

How does this fit into the PHP Zone?
0
 
ITsolutionWizardAuthor Commented:
Regardless, I want to use my existing codes base. Please continue if you know how.
0
 
Slick812Commented:
greetings ITsolutionWizard, , In the code for the w3shools web page you gave they have this -
    xmlDoc=xmlhttp.responseXML;

as what I consider to be the "Return" from an XML ajax request made with this -
     xmlhttp.open("GET","cd_catalog.xml",false);

so I can not understand what you mean, OR what you might need when you say - "return to array in javascript."
you say the result would be like -
     Array("data1","data2")

but as you may know, , any AJAX return is just a "text" output from the server, if you use the -
   xmlhttp.responseXML
then the javascript will use the text as a XML DOM object with properties, So if you use the XML as the w3schools does, then you will have to use the XML tags to assign values, but you do not get an array.

If you abandon the XML ajax formatting, then you can get a CSV delimited text, and use -
    ajax.responseText.split("\n");
to get an array.
0
 
ITsolutionWizardAuthor Commented:
Then just send me example from xml string to array written in JavaScript
0
 
Slick812Commented:
you say you want an array fron XML? ?, below is untested code , that I just wrote, for here, this -
for (i=0;i<x.length;i++)
  {
  array1.push( x[j].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue );
  }

should place the artist values from the XML into array1
<script>
var xmlhttp = false;
/*@cc_on
@if (@_jscript_version>=5)try{xmlhttp = new ActiveXObject('Msxml2.XMLHTTP.4.0');} catch(e){
	try{xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} catch(e){
	try{xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");} catch(E){xmlhttp=false;}}}
	@else xmlhttp = false;
@end @*/
if(!xmlhttp && typeof XMLHttpRequest !='undefined'){try{xmlhttp = new XMLHttpRequest();} 
catch(e){xmlhttp = false;}}

array1 = [];

xmlhttp.open("GET","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;


var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
  {
  array1.push( x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue );
  }

document.write(array1[0]);
</script>

Open in new window


hopefully you can see some of what this does, ask questions if you need more info.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now