Read XML file using JavaScript in Chrome

31.8k views Asked by At

I need to load and read an XML file using JavaScript.

The following code works fine in Firefox, IE and Opera:

function loadXMLDoc(dname) {
  var xmlDoc

  // Internet Explorer
  try {
    xmlDoc = new ActiveXObject('Microsoft.XMLDOM')
  }
  catch (e) {
    // Firefox, Opera, etc.
    try {
      xmlDoc = document.implementation.createDocument('', '', null)
    }
    catch (e) {
      alert(e.message)
    }
  }

  try {
    xmlDoc.async = false
    xmlDoc.load(dname)
    return xmlDoc
  }
  catch (e) {
    alert(e.message)
  }

  return null
}

But executing this code in Chrome gives me this error:

Object# has no method "load"

4

There are 4 answers

1
kube On BEST ANSWER

Legacy Code

document.implementation.createDocument does not work on Chrome and Safari.

Use XMLHttpRequest instead when possible:

function loadXMLSync(url) {
  try {
    // Prefer XMLHttpRequest when available
    var xhr = new XMLHttpRequest()
    xhr.open('GET', url, false)
    xhr.setRequestHeader('Content-Type', 'text/xml')
    xhr.send()

    return xhr.responseXML
  }
  catch (e) {
    // XMLHttpRequest not available, fallback on ActiveXObject
    try {
      var activex = new ActiveXObject('Microsoft.XMLDOM')
      activex.async = false
      activex.load(url)

      return activex
    }
    catch (e) {
      // Neither XMLHttpRequest or ActiveXObject are available
      return undefined
    }
  }
}

Modern Browsers

If you're targeting modern browsers (> IE6), just use XMLHttpRequest:

function loadXMLSync(url) {
  var xhr = new XMLHttpRequest()

  xhr.open('GET', url, false)
  xhr.setRequestHeader('Content-Type', 'text/xml')
  xhr.send()

  return xhr.responseXML
}
0
Thomas Tregner On

On MDN, there is guidance to use XMLHttpRequest. But it isn't clear from DOMImplementation.createDocument until you drill into the return type and see that XMLDocument is not supported in Google Chrome. The example on W3Schools uses XMLHttpRequest.

0
galian On

Add

    var xhr = new XMLHttpRequest(); 
    xhr.open("GET", "/example/xdom/books.xml", false); 
    xhr.send(null); 
    xmlDoc = xhr.responseXML.documentElement; 
    return xmlDoc;

in catch statement. Like below:

function loadXMLDoc(dname) {
  var xmlDoc

  // Internet Explorer
  try {
    xmlDoc = new ActiveXObject('Microsoft.XMLDOM')
  }
  catch (e) {
    // Firefox, Opera, etc.
    try {
      xmlDoc = document.implementation.createDocument('', '', null)
    }
    catch (e) {
      alert(e.message)
    }
  }

  try {
    xmlDoc.async = false
    xmlDoc.load(dname)
    return xmlDoc
  }
  catch (e) {
    //alert(e.message)
    // For Chrome 
    var xhr = new XMLHttpRequest(); 
    xhr.open("GET", "/example/xdom/books.xml", false); 
    xhr.send(null); 
    xmlDoc = xhr.responseXML.documentElement; 
    return xmlDoc;
  }

  return null
}
0
Jose Kj On

follow this to print,load,append xml data.Here xml is stored as string inside javascript.This method works in chrome,firefox hopes it will work in others too

txt="<papers>"+"<paper>"+
 "<author>athor name</author>"+
"<title>title</title>"+
"<path>path</path>"+
"<track>which tack</track>"+
 "</paper>"+
"<paper>"+
"<author>athor name</author>"+
"<title>title</title>"+
"<path>path</path>"+
"<track>which tack</track>"+
"</paper>"+
"<paper>"+
"<author>athor name</author>"+
"<title>title</title>"+
"<path>path</path>"+
"<track>which tack</track>"+
"</paper>"+
"<papers>";
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);
    }

x=xmlDoc.getElementsByTagName("paper"); 
for (var i = 0; i < x.length; i++) {  
    var athor =x[i].childNodes[0].firstChild.nodeValue;
    var title = x[i].childNodes[1].firstChild.nodeValue;
    var path = x[i].childNodes[2].firstChild.nodeValue;
    var tack =x[i].childNodes[3].firstChild.nodeValue;
    //do something with these values...
    //each iteration gives one paper details    
    var xml=document.getElementById("element_id");//<div id="element_id"></div>
    var li = document.createElement("br");// create a new <br>  
    newlink = document.createElement('A'); // creating an <a> element
    newlink.innerHTML = athor;// adding <a>athor value here</a>
    newlink.setAttribute('href', path);// <a href="path"></a>

    newlink.appendChild(li);// <a href="path">athor</a><br>
    document.getElementById("element_id").appendChild(newlink);
//finaly it becomes <div id="element_id"><a href="path">athor</a><br></div>


}

i posted this answer here