Jump to content

parse an xml file


securobo2

Recommended Posts

I don't have time to go through an entire Javascript tutorial for this. Can someone give a quick simple code snippet for this?I would like to use an xml file with about 100 entries. Each entry is as follows.

titleauthorrelease date

I would like to display the entire file in an html page. I don't need help with html or css, I can markup my page and etc.Could someone please give an example of how to load that xml file and display each entry, after one another. so it looks like this.

title, author, release datetitle, author, release datetitle, author, release date...

On w3schools there is an xmlparser example, but it does not illustrate displaying more than one item. This needs loops and etc. I don't want to go through an entire tutorial for this.please help.Thanks anyone that can take their time on this.

Link to comment
Share on other sites

I don't have time to go through an entire Javascript tutorial for this. Can someone give a quick simple code snippet for this?I would like to use an xml file with about 100 entries. Each entry is as follows.
titleauthorrelease date

I would like to display the entire file in an html page. I don't need help with html or css, I can markup my page and etc.Could someone please give an example of how to load that xml file and display each entry, after one another. so it looks like this.

title, author, release datetitle, author, release datetitle, author, release date...

On w3schools there is an xmlparser example, but it does not illustrate displaying more than one item. This needs loops and etc. I don't want to go through an entire tutorial for this.please help.Thanks anyone that can take their time on this.

one way is to use ajax when the page load //create the xhr objectfunction createXHR(){ if (typeof XMLHttpRequest != "undefined"){ return new XMLHttpRequest(); } else if (typeof ActiveXObject != "undefined"){ if (typeof arguments.callee.activeXString != "string"){ var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"]; for (var i=0,len=versions.length; i < len; i++){ try { var xhr = new ActiveXObject(versions); arguments.callee.activeXString = versions; return xhr; } catch (ex){ //skip } } } return new ActiveXObject(arguments.callee.activeXString); } else { throw new Error("No XHR object available."); } }var xhr = createXHR();//get the xml filefunction getXml(){ if(xhr.overrideMimeType){ xhr.overrideMimeType('text/xml'); } xhr.open('get','file.xml',true); //you need to chane file.xml to your file name xhr.onreadystatechange = function(){ if(xhr.readyState == 4 && xhr.status == 200){ var xml = xhr.responseXML; //get your elements var authors = xml.getElementsByTagName('author'); var titles = xml.getElementsByTagName('title'); var release_date = xml.getElementsByTagName('realease_date'); //loop and view the result for(var i=0; autors; i++){ var author = authors.firstChild.nodeValue; var title = titles.firstChild.nodeValue; var date = release_date.firstChild.nodeValue; var content = "author: " + author + "" + "Tilte: " + title + "" + "Date: " + date ; result.innerHTML = content; } } }; xhr.send(null) }}window.onload = getXml; i hope that help
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...