Game Development Community

XMLObject - TGB/TGE

by David Higgins · 12/25/2006 (3:40 am) · 1 comments

XMLObject for TGB/TGE using Expat



I put together this little resource for a prototype I'm working on, and figured I'd share the resource with the community.

Basically, it's just a simple "XMLObject" in C++, exposed to TorqueScript. It utilizes the Expat XML Library to perform SAX parsing of XML documents.

I exposed two methods to the console, which are "parse" and "parseDocument". Both methods take in a single string;

void parse(const char *data);
void parseDocument(const char *data);

If you have a string of XML you would like to parse, such as a string obtained from a network resource (SOAP, XML-RPC) you can pass the string to parse(), but if you have an existing XML document you can call the parseDocument and pass a filename.

Both functions initiate the generic Expat SAX callbacks for element triggers, these callbacks are as follows (these are the TorqueScript methods in -your- code that are called from the object);

onDefault(%data, %len)
onElementStart(%name, %attributes)
onElementEnd(%name)
onCharacterData(%name, %data)
onProcessingInstruction(%target, %data)
onComment(%data)
onCdataSectionStart()
onCdataSectionEnd()

%name is always the name of the element that the remaining data is contained in
%data is always free-form data, always in the form of a string
%attributes is a SimSet containing SimObjects with Dynamic Fields for AttributeName and AttributeValue
%len is an integer denoting the length of %data
%target is unknown, specific to the Processing Instruction and taken right from Expat (untested).

Included in the ZIP file are;

engine/source/ZoulCreations/XMLObject.cc
engine/source/ZoulCreations/XMLObject.h
engine/lib/expat2/*.h
engine/lib/expat2/libexpatMT.lib
games/XMLObjectDemo

To get this resource working, you need to make some modifications to your project, in my case, I modified the VS2005 Solution, and will post an update to this resource at a later date with Makefile modifications that work on all platforms (if anyone else wants to before then, thats fine).

Here are the modifications i made;

Extract the resource into your TGB directory, it should put the files where they need to go;


  1. Open your T2D solution in VS2005 (2003 should work the same?)
  2. Add a new "Filter" to your T2D project, call it "ZoulCreations"
  3. Add two existing files to this Filter, XMLObject.cc and XMLObject.h (locate them in engine/source/ZoulCreations)
  4. Edit the T2D Project's Properties
  5. Goto the Linker->General Properties
  6. Add "../../lib/expat2/" to the "Additional Library Directories"
  7. Goto the Linker->Input Properties
  8. Add "libexpatMT.lib" to "Additional Dependencies"
  9. Add "MSVCRTD" to "Ignore Specific Library"

Save changes, and rebuild the T2D Project.

The expat library can be obtained from expat.sourceforge.net/

All I did was compile the expat library first, then linked to the pre-compiled static expat lib I created.

In Linux and OSX, I would assume you can just simply build the static expat library and place it in your standard 'lib' directory.


Download Here


David Higgins
Programmer
Gear Worx Productions


#1
12/29/2006 (8:06 pm)
Looks fantastic!

This seems much simpler and more bare-bones than the XML Console Parser resource.

Thanks so much for sharing this with the community! This might be just what I need...

--clint