1 / 11

JS and XML

JS and XML. An example from JavaScript: (Powell, Schneider) the complete reference. Loading XML into IE is not exactly the same as in Mozilla. in IE , an XMLDoc object is created to manipulate/navigate the object rather than a (html) document object. Sample xml for this in slide notes.

Download Presentation

JS and XML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JS and XML

  2. An example from JavaScript: (Powell, Schneider) the complete reference • Loading XML into IE is not exactly the same as in Mozilla. • in IE , an XMLDoc object is created to manipulate/navigate the object rather than a (html) document object. • Sample xml for this in slide notes

  3. IE example • Script in slide notes • Does not work in Mozilla • Load,edit (add/delete rec) xml file • Save xml file not possible as far as I could tell

  4. Editing xml in browser window

  5. Same in Mozilla? • I have not (yet) got it working.

  6. mozilla

  7. The script- in slide notes <html> <head> <SCRIPT language="JavaScript"> function importXML() { if (document.implementation && document.implementation.createDocument) { xmlDoc = document.implementation.createDocument("", "", null); xmlDoc.onload = createTable; } else if (window.ActiveXObject) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.onreadystatechange = function () { if (xmlDoc.readyState == 4) createTable() }; } else { alert('Your browser can\'t handle this script'); return; } xmlDoc.load("staff.xml"); }

  8. continued function createTable() { var x = xmlDoc.getElementsByTagName('employee'); var newEl = document.createElement('TABLE'); newEl.setAttribute('cellPadding',5); var tmp = document.createElement('TBODY'); newEl.appendChild(tmp); var row = document.createElement('TR'); for (j=0;j<x[0].childNodes.length;j++) { if (x[0].childNodes[j].nodeType != 1) continue; var container = document.createElement('TH'); var theData = document.createTextNode(x[0].childNodes[j].nodeName); container.appendChild(theData); row.appendChild(container); } tmp.appendChild(row); for (i=0;i<x.length;i++) { var row = document.createElement('TR'); for (j=0;j<x[i].childNodes.length;j++) { if (x[i].childNodes[j].nodeType != 1) continue; var container = document.createElement('TD'); var theData = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue); container.appendChild(theData); row.appendChild(container); } tmp.appendChild(row); } document.getElementById('writeroot').appendChild(newEl);}

  9. continued </script> <body onload="importXML()"> <div id='writeroot'> the table was created dynamically.</div> </body> </html>

  10. And you can add another element function addElt(){ var rootlist=xmlDoc.getElementsByTagName('directory'); if(rootlist.length==1) rootElt=rootlist.item(0); else alert("empty employee list"); var name=prompt("name"); var title=prompt("title");; var email=prompt("email");; var phone=prompt("phone");; var newEmpl=xmlDoc.createElement('employee'); var newName=xmlDoc.createElement('name'); var newTitle=xmlDoc.createElement('title'); var newPhone=xmlDoc.createElement('phone'); var newEmail=xmlDoc.createElement('email'); var newTitleText=xmlDoc.createTextNode(title); var newNameText=xmlDoc.createTextNode(name); var newEmailText=xmlDoc.createTextNode(email); var newPhoneText=xmlDoc.createTextNode(phone); newName.appendChild(newNameText); newTitle.appendChild(newTitleText); newEmail.appendChild(newEmailText); newPhone.appendChild(newPhoneText); newEmpl.appendChild(newName); newEmpl.appendChild(newTitle); newEmpl.appendChild(newPhone); newEmpl.appendChild(newEmail); rootElt.appendChild(newEmpl);}

  11. Note new table with new guy

More Related