A Kick-Start to SAX with C++, Part 3

This is the third and last article of this series. In the first article, you saw what SAX is, what the Microsoft COM implementation of SAX is, and how to write a simple parser of a XML document. In the second article, you saw how to work around the limitation of having a single handler type registered to the XML reader at a time. In both articles, you ignored anything related to errors. Now, you will see how to implement some simple error handling. Before going any further, I recommend that you read the first and second parts, if you haven’t already.

Microsoft COM Support for Errors Handling

As seen in the previous articles, you can register to the XML reader a content handler, a DTD handler (which will not be covered), and an error handler. In the COM implementation of SAX, ISAXErrorHandler is the interface that provides the error handling functionality.

ISAXErrorHandler has three methods. All of them take the same three parameters:

Parameter Description
ISAXLocator * pLocator Pointer to a locator object that contains information about the file, line, and column where the error occurred
const wchar_t * pwchErrorMessage Textual information about the error
HRESULT hrErrorCode Error code identifying the reason of the error

The three methods of ISAXErrorHandler are:

Method Description
error() Receives notification of a recoverable error
fatalError() Receives notification of non-recoverable errors
ignorableWarning() Receives notifications of warnings

However, in the current implementation all warnings are treated as errors and all errors are non-recoverable. Thus, only fatalError() is called in the current implementation.

ISAXLocator associates a SAX event with a document location. The information stored in the locator is updated for each event. The interface has four methods (table taken from MSDN):

Method Description
getColumnNumber Returns the column number where the current document event ends
getLineNumber Returns the line number where the current document event ends
getPublicId Returns the public identifier for the current document event
getSystemId Returns the system identifier for the current document event

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read