120 likes | 260 Views
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.
E N D
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
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
Same in Mozilla? • I have not (yet) got it working.
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"); }
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);}
continued </script> <body onload="importXML()"> <div id='writeroot'> the table was created dynamically.</div> </body> </html>
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);}