untidy.net :: untidy c++ :: XMLParser(Last Update: 7th April 2003)

This is a simple set of wrapper classes for the Expat XML parser, originally written by James Clark. This is the XML parser used as the basis of the XML support in many libraries - I know for sure that it has been used in at least python and perl. It is a very tightly written piece of C code and results in amazingly fast XML parsing. Expat is a non-validating parser and it works using the event-driven SAX parsing methodology. When expat finds new tags or character data in the XML data it calls user-defined events. This code provides a simple way to define what methods are called, and to wrap all of the functionality up in c++ classes.

Usage:

Simply create an XMLParser object where you wish to begin parsing. You will need to derive a class from XMLParseState or use the template version to point at class member functions. With either of these, you must then call XMLParser.SetParseState and give it a pointer to an instance of the class which implements XMLParseState. Finally, call either Load(filename) or LoadFragment(xml). The events you have overridden in your XMLParseState sub-class or provided for the templated version will be called at the appropriate time. Load or LoadFragment will return when all the data has been parsed.

If there is a parse error, XMLParser will throw an instance of XMLParseException which has methods allowing you to find out what the problem was, and where in the data stream it occurred. It is always advisable to write code so that it can catch these exceptions if and when they occur.

This code is all written using TCHAR and the related wrapper functions provided by Microsoft's runtime libraries. For those that do not have these - such as those using GCC on linux or windows, I will soon provide a header file providing the same functionality.

note: This code currently depends on the included CFile class to provide file reading functionality. I will remove this dependancy in the future.

Download:

Download Source (BSD license, c++)