JavaScript/Handling XML

From Wikibooks, open books for an open world
< JavaScript
Jump to: navigation, search

A simple function to open an xml file (it first try for IE then FF and others):

function loadXMLDoc(xmlfilename) {
var e = new Error();
// Internet Explorer
try {
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
}
catch(e) {
// Firefox, Mozilla, Opera, others

           try {

xmlDoc = document.implementation.createDocument("","",null);
}

catch(e) {
throw(e.message);
}
}

try {
xmlDoc.async = false;

xmlDoc.load(xmlfilename);
return(xmlDoc);
}
catch(e) {
throw(e.message);
}
return(null);
}


Usage:
var objXML = loadXMLDoc("filename.xml");
var oNodes = objXML.getElementsByTagName("AnyTagYouWish");


Now you can do any DOM operations on oNodes.

XML modifications can't be saved in JavaScript as this is clientside...