Read XML file using JavaScript in Chrome

人走茶凉 提交于 2019-11-30 14:33:31

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 xmlhttp.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
}

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.

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

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!