Listing 1: XMLInterpreter implements the DocumentHandler interface.
class XMLInterpreter : private DocumentHandler
{
private:
// The document handler.
IXMLDocumentHandler *m_documentHandler;
// A stack of element handlers.
std::stack< IXMLElementHandler * > m_elementHandlers;
/* ... */
public:
XMLInterpreter( IXMLDocumentHandler *documentHandler );
void parse( const char *const systemID );
private:
// DocumentHandler implementation.
void startElement (const XMLCh *const name, AttributeList &attrs);
void endElement (const XMLCh *const name);
void characters (const XMLCh *const chars, const unsigned int length);
/* ... */
};
XMLInterpreter::XMLInterpreter( IXMLDocumentHandler *documentHandler ) :
m_documentHandler( documentHandler ) /* ... */
{
}
void XMLInterpreter::parse( const char *const systemID )
{
// Create a SAX parser, and parse the document.
SAXParser parser;
parser.setDocumentHandler( this );
parser.parse( systemID );
}
void XMLInterpreter::startElement (const XMLCh *const name, AttributeList &attrs)
{
// If we are parsing an element, use its handler.
// Otherwise, use the documents handler itself.
IXMLElementContainerHandler *top;
if ( !m_elementHandlers.empty() )
top = m_elementHandlers.top();
else
top = m_documentHandler;
// Obtain the element handler of the new element from the current scope.
IXMLElementHandler *newElement = top->child( name );
m_elementHandlers.push( newElement );
// Notify the element handler of the start of the element.
newElement->start( attrs );
}
void XMLInterpreter::endElement (const XMLCh *const name)
{
// Notify the top element handler of the end of the element, then
// pop it, revealing the handler of it's container.
m_elementHandlers.top()->end();
m_elementHandlers.pop();
}
void XMLInterpreter::characters (const XMLCh *const chars,
const unsigned int length)
{
// Notify the top element handler of the characters.
m_elementHandlers.top()->characters( chars, length );
}