Read XML file using JavaScript in Chrome

前端 未结 4 1238
栀梦
栀梦 2021-01-02 18:03

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

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

         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 18:28

    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=""+""+
     "athor name"+
    "title"+
    "path"+
    "which tack"+
     ""+
    ""+
    "athor name"+
    "title"+
    "path"+
    "which tack"+
    ""+
    ""+
    "athor name"+
    "title"+
    "path"+
    "which tack"+
    ""+
    "";
    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");//
    var li = document.createElement("br");// create a new
    newlink = document.createElement('A'); // creating an element newlink.innerHTML = athor;// adding athor value here newlink.setAttribute('href', path);// newlink.appendChild(li);// athor
    document.getElementById("element_id").appendChild(newlink); //finaly it becomes }

    i posted this answer here

提交回复
热议问题