Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I need to be able to parse XML using JavaScript. The XML will be in a variable. I would perfer not to use jQuery or other frameworks.
I have looked at this, XML > jQuery reading.
Thanks

share|improve this question

marked as duplicate by Vivin Paliath, maerics, acdcjunior, Mike Samuel, animuson Jul 12 '13 at 0:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
/s/Phrase/parse? –  Brad Jul 11 '13 at 21:46
    
Did you mean parse? –  Vivin Paliath Jul 11 '13 at 21:47
    
Are you trying to parse (read and convert) XML or phrase (write in correct syntax) XML? –  Enigmadan Jul 11 '13 at 21:48
    
I may be going to use PhoneGap to package the app and I don't know if that will work. –  user2574350 Jul 11 '13 at 21:57
    
I Meant Parse, not phrase, I edited it. –  user2574350 Jul 11 '13 at 21:57

2 Answers 2

up vote 12 down vote accepted

I'm guessing from your last question, asked 20 minutes before this one, that you are trying to parse (read and convert) the XML found through using GeoNames' FindNearestAddress.

If your XML is in a string variable called txt and looks like this:

<address>
 <street>Roble Ave</street>
  <mtfcc>S1400</mtfcc>
  <streetNumber>649</streetNumber>
  <lat>37.45127</lat>
  <lng>-122.18032</lng>
  <distance>0.04</distance>
  <postalcode>94025</postalcode>
  <placename>Menlo Park</placename>
  <adminCode2>081</adminCode2>
  <adminName2>San Mateo</adminName2>
  <adminCode1>CA</adminCode1>
  <adminName1>California</adminName1>
  <countryCode>US</countryCode>
 </address>

Then you can parse the XML with Javascript DOM like this:

if (window.DOMParser)
  {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(txt,"text/xml");
  }
else // Internet Explorer
  {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.loadXML(txt);
  }

And get specific values from the nodes like this:

//Gets house address number
xmlDoc.getElementsByTagName("streetNumber")[0].childNodes[0].nodeValue;

//Gets Street name
xmlDoc.getElementsByTagName("street")[0].childNodes[0].nodeValue;

//Gets Postal Code
xmlDoc.getElementsByTagName("postalcode")[0].childNodes[0].nodeValue;

JSFiddle

share|improve this answer
    
Perfect, Thanks A Lot –  user2574350 Jul 11 '13 at 22:05

The following will parse an XML string into an XML document in all major browsers, including Internet Explorer 6. Once you have that, you can use the usual DOM traversal methods/properties such as childNodes and getElementsByTagName() to get the nodes you want.

var parseXml;

if (typeof window.DOMParser != "undefined") {
    parseXml = function(xmlStr) {
        return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" &&
       new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    throw new Error("No XML parser found");
}

Example usage:

var xml = parseXml("<foo>Stuff</foo>");
alert(xml.documentElement.nodeName);

Which I got from http://stackoverflow.com/a/8412989/1232175.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.