Skip to content Skip to sidebar Skip to footer

Parsing XML In Javascfipt

Ok I am trying to parse an XML from a get request in Javascript. This is how the XML looks like

Solution 1:

Try this:

var markers = xml.documentElement.getElementsByTagName("marker");

var data = [];

for (var i=0;i<markers.length;i++) {
    console.log(markers[i].getElementsByTagName("name")[0]);
    data.push({
        name: markers[i].getElementsByTagName("name")[0].textContent,
        address: markers[i].getElementsByTagName("address")[0].textContent 
    })
}
// data is now [{name: "...", address: "..." }]

See working fiddle: http://jsfiddle.net/nrabinowitz/XZnXn/

For more on navigating a DOM structure in Javascript, you might want to look at the MDN DOM Reference.


Post a Comment for "Parsing XML In Javascfipt"