I need to load and read an XML file using JavaScript.
The following code works fine in Firefox, IE and Opera:
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 "+
""+
" "+
""+
"athor name "+
"title "+
"path "+
""+
" "+
""+
"athor name "+
"title "+
"path "+
""+
" "+
"";
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