Last modified on 18 March 2015, at 12:03

JavaScript/Handling XML

Previous: Handling JSON Index Next: Working With Files

Simple function to open an XML fileEdit

This function first tries for Microsoft Internet Explorer, then for Firefox 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);
}

UsageEdit

var objXML = loadXMLDoc("filename.xml");
var oNodes = objXML.getElementsByTagName("AnyTagYouWish");</code>

Now you can do any DOM operations on oNodes.

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


Previous: Handling JSON Index Next: Working With Files