0

Can somebody tell me how to parse information from XML to array. So I have an XML that looks like this:

<name>
  <gps>coordinate1</gps>
  <gps>coordinate2</gps>
  ...
</name>
...

So I would like to pull out the value of the name tag, put it into a variable, and then pull out the value of the gps tag, basically the coordinates and store them into an array.

Dominik
  • 311
  • 1
  • 4
  • 11
  • 1
    possible duplicate of [XML parsing of a variable string in JavaScript](http://stackoverflow.com/questions/649614/xml-parsing-of-a-variable-string-in-javascript) – LJᛃ Dec 24 '14 at 02:41
  • @LJ: That might be part of the answer, but I see most of the question as being how to extract the data from the document once you have it in DOM form. – icktoofay Dec 26 '14 at 01:53

1 Answers1

0

Try this:

var parser = new DOMParser();
            parser.async="false";
            var xmlDoc = parser.parseFromString ("<name><gps>a</gps><gps>b</gps></name>", "text/xml");
            var nameNode = xmlDoc.getElementsByTagName("name")[0];
            var result = new Array();
            for(var i = 0; i< nameNode.childNodes.length; i++) {
                var gpsNode = nameNode.childNodes[i];
                result.push(gpsNode.innerHTML);
            } 

            console.log(result);
Howard
  • 3,638
  • 4
  • 32
  • 39
  • When I try the following ajax call: var response = ''; $.ajax({ type: "GET", url: "test.php", async: false, success : function(text) { response = text; } }); I can not parse with the code that you provided. – Dominik Dec 29 '14 at 06:02