TorqueScript XMLObject

I have started working on an implementation of Expat (the Gnome XML Parser) library into TGB, and expect that a large portion of the code should work with TGE as well, as it’s just a simple Console Object being added to the system.

So far, I’ve managed to create and expose the XMLObject class to TorqueScript, as well as added a ‘parseDocument’ method to the object, which is exposed to TorqueScript and takes in a Filename as it’s only parameter. This then makes numerous calls to the startElement and endElement C++ methods (there’s a static and non-static version of each, something to do with the function pointers that expat uses since its written in C). These C++ methods then call there TorqueScript equivalents, onStartElement and onEndElement, which allows a script developer to very simply use the SAX parser functionality of Expat within TorqueScript by doing the following:

[js]
function MySax::onStartElement(%this, %name, %atts)
{
echo(“ElementStart: ” @ %name);
for(%x = 0; %x < %atts.getCount(); %x++)
{
echo(” Name: ” @ %atts.getObject(%x).AttributeName);
echo(” Value: ” @ %atts.getObject(%x).AttributeValue);
}
}

function MySax::onElementEnd(%this, %name)
{
echo(“TS ElementEnd: ” @ %name);
}

%sax = new XMLObject()
{
class = “MySax”;
};

%sax.parseDocument(“XMLFile.xml”);
[/js]

I am intending to release the XMLObject code, along with any other TGB engine hacks I make that use the code, as a resource on GG’s site. I’ll post my progress here, and when I get a more completed version, I’ll post a .plan on GG’s site.

So far, all I’ve managed to cover is the Start and End element handlers, and the CDATA handler for the XML, my plans include to add the following:

  1. SimObject.saveXml() — save SimObjects as XML Files rather then TorqueScript
  2. SimObject.loadXml() — load SimObjects from XML Files, rather then exec()ing the TorqueScript
  3. Full list of SAX Methods, as made available through Expat
  4. Possibility of adding a DOM to the Code (Adding a DOM may require using yet another library, other then SAX, or a replacement to SAX that supports DOM — but I’m going to take a look at implementing a very simple DOM using my own code, and my own XPath implementation — just cause I wanna see how hard it’ll be to do ;p)

Leave a Reply