Parse XML using JavaScript

Parse XML using JavaScript

Asked on January 8, 2019 in XML.
Add Comment


  • 7 Answer(s)

    Parsing xml using javascript :

         If the XML is in string variable that is called as txt , that will be like,

    <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 we can parse the XML with Javascript DOM like below,

    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);
    }
    

         Atlast get specific values from the nodes like,

    //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

    Answered on January 8, 2019.
    Add Comment

         To parse an XML string into an XML document in all major browsers, including Internet Explorer 6 that once we should have that, using the usual DOM traversal methods or properties like childNodes and getElementsByTagName() to get the nodes that is needed.

    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");
    }
    

         For instance:

    var xml = parseXml("<foo>Stuff</foo>");
    alert(xml.documentElement.nodeName);
    
    Answered on January 8, 2019.
    Add Comment

    The XML DOM (Document Object Model) defines the properties and methods for accessing and editing XML.

    However, before an XML document can be accessed, it must be loaded into an XML DOM object.

    All modern browsers have a built-in XML parser that can convert text into an XML DOM object.

    Answered on February 28, 2019.
    Add Comment
    <html>
    <body>

    <p id=”demo”></p>

    <script>
    var text, parser, xmlDoc;

    text = “<bookstore><book>” +
    “<title>Everyday Italian</title>” +
    “<author>Giada De Laurentiis</author>” +
    “<year>2005</year>” +
    “</book></bookstore>”;

    parser = new DOMParser();
    xmlDoc = parser.parseFromString(text,”text/xml”);

    document.getElementById(“demo”).innerHTML =
    xmlDoc.getElementsByTagName(“title”)[0].childNodes[0].nodeValue;
    </script>

    </body>
    </html>

    Answered on February 28, 2019.
    Add Comment

    if (window.DOMParser) {
    // code for modern browsers
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(text,”text/xml”);
    } else {
    // code for old IE browsers
    xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);
    xmlDoc.async = false;
    xmlDoc.loadXML(text);
    }

    Answered on February 28, 2019.
    Add Comment

    xmlDoc = xmlhttp.responseXML;
    txt = “”;
    x = xmlDoc.getElementsByTagName(“ARTIST”);
    for (i = 0; i < x.length; i++) {
    txt += x[i].childNodes[0].nodeValue + “<br>”;
    }
    document.getElementById(“demo”).innerHTML = txt;

    Answered on February 28, 2019.
    Add Comment

    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;
    Answered on March 5, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.