JavaScript/Handling XML
Previous: Handling JSON Index Next: Working with files

Simple function to open an XML file

This function first tries for Microsoft Internet Explorer, then for Firefox and others:

function loadXMLDoc(xmlfilename) {
  var event = new Error;
  // Internet Explorer
  try {
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  } catch(event) {
    // Firefox, Mozilla, Opera, others
      try {
      xmlDoc = document.implementation.createDocument("","",null);
    } catch(event) {
      throw(event.message);
    }
  }

  try {
    xmlDoc.async = false;

    xmlDoc.load(xmlfilename);
    return(xmlDoc);
  } catch(event) {
    throw(event.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...


Previous: Handling JSON Index Next: Working with files

  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.


JavaScript/Handling_XML
 

Manage research, learning and skills at IT1me. Create an account using LinkedIn to manage and organize your IT knowledge. IT1me works like a shopping cart for information -- helping you to save, discuss and share.


  Contact Us  |  About IT1me.com |  IT Training & References |  IT Careers |  IT Hardware |  IT Software |  IT Books