Jump to content
  • Advertisement
Sign in to follow this  
gretty

How to correct Javascript Prototype that inherits from XMLDocument

This topic is 2597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello

Ok I've tried to apply the advice about how to create..prototypes(I think thats what it's called) in Javascript thanks to Gamer Gamester's advice. Hopefully it follows the conventions of Javascript.

What should be changed so that it works :P & so thats its correct javascript coding? The code doesn't compile/work right now :P

I am attempting to create a Cross Browser XML parser. So I try to "subclass" the XMLDocument object, although I am unsure if that object even exists, I am trying to subclass the object (returned from XMLHttpRequest() object) when I call the function xmlHttp.responseXML;

One important thing is I want to stick to native Javascript & steer clear of EcmaScript 5 for now until I learn the native javascript ways to create prototypes & perform inheritence.



// I intend to use the XMLHandler object like so:
var xml = XMLHandler("myXMLFile.xml");
var slides = xml.getElementsByTageName("slide");

function XMLHandler( /*string*/ xmlFilePath )
{
this.getXMLFile = function()
{
return this.xmlFile;
}

this.xmlFile = xmlFilePath;
this.parseXMLFile( this.xmlFile );
}


XMLHandler.prototype = new XMLDocument(); // is this enough to make the object inherit from the XMLDocument
XMLHandler.prototype.constructor = XMLDocument; // make XMLHandler call the base class constructor when created


if ( window.XMLHttpRequest )
{
XMLHandler.prototype.parseXMLFile = function( xmlFilePath )
{
this.xmlFile = xmlFilePath;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", this.xmlFile, false); //Open the file using the GET routine
xmlHttp.send(null); //Send request
this = xmlHttp.responseXML; //this object holds/is the document information now
}
}
else if ( window.ActiveXObject ) // if the current browser is an old version of IE
{
XMLHandler.prototype.parseXMLFile = function( xmlFilePath )
{
this.xmlFile = xmlFilePath;
var xmlHttp = new ActiveXObject("Microsoft.XMLDOM");
xmlHttp.async = "false"; // keep synchronous for now
this = xmlHttp.load( this.xmlFile );
}
}



Share this post


Link to post
Share on other sites
Advertisement
XML is notoriously annoying to parse in JS. JSON effectively took over that role.

Main issue with XML is its complexity. While it's possible to parse trivial or XML-like documents, full parser needs to do all the schema transforms and validation, something that brings Java applications to its knees and requires huge libraries.

The usual way to parse such documents is by using browser's DOM model into which the XML is converted. But, as always, there are arbitrary inconsistencies. Older versions of IE required extra package to support it, not every detail is handled in same manner, especially once schemas get involved and working with XML hierarchies tends to be tedious.

At minimum, look into jQuery's XML parser, its selectors restore a lot of sanity.
Similar for AJAX calls - there are well tested libraries which create boilerplate for making such calls that correctly handle browser inconsistencies. And despite having XML in the name, AJAX calls can request any type of content.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!