2

hey guys.. i was wondering what the best way to go about getting a node from an xml doc into a javascript variable would be? i dont really want to use jquery because i believe that i wont have too.. so far i have:

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","sc2cash.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

but now how i select the nodes? thanks

3
  • not sure there's any good way of parsing XML in js without an external library. when you use ajax it's not typical to send the data as xml, usually it's sent as json Commented Jun 2, 2011 at 19:54
  • well maybe you can help me understand how it works with jquery? Commented Jun 2, 2011 at 19:57
  • AJAX stands for Asynchronous Javascript and XML, and until the last 2 years or so, it was totally commonplace to use XML instead of json, and to do it without a library. The more time I spend on SO, the more it feels like writing plain old javascript like this has become a lost art :) Commented Jun 2, 2011 at 20:14

1 Answer 1

2

You can parse the returned XML using DOM manipulators. We'll need more information about the XML returned to be more specific.

xmlDoc=xmlhttp.responseXML;

// For example - get "person" tags if there were any
var people = xmlDoc.getElementsByTagName("person");

// All child nodes under the first person
var children = people[0].childNodes;
Sign up to request clarification or add additional context in comments.

2 Comments

is there any way to do something like this? xmlDoc.getElementsByTagName("person")[0];
whoa, other shawn here, had no idea you could do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.