Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / XML

Parsing XML using a C++ wrapper for SAX2

Rate me:
Please Sign up or sign in to vote.
4.75/5 (25 votes)
3 Aug 2005CPOL15 min read 375.5K   2.9K   77  
A basic C++ wrapper framework for the MSXML SAX2 API is presented.
// Filename: XmlElementHandler.h
// 2005-07-15 nschan Initial revision.
// 2005-07-28 nschan Added IWXmlElementHandler interface for wide-char string support.

#ifndef XML_ELEMENT_HANDLER_INCLUDED
#define XML_ELEMENT_HANDLER_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "XmlDataTypes.h"
#include <string>

// IXmlElementHandler (single-char string version)
// An application-specific class should implement this
// interface in order to handle XML events that are
// raised during parsing.
class IXmlElementHandler
{
public:
    // Handle XML content events during parsing.
    virtual void OnXmlStartElement(const CXmlElement& xmlElement) = 0;
    virtual void OnXmlElementData(const std::string& elementData, int depth) = 0;
    virtual void OnXmlEndElement(const CXmlElement& xmlElement) = 0;
    
    // Handle XML error events during parsing.
    virtual void OnXmlError(int line, int column, const std::string& errorText, unsigned long errorCode) = 0;
    
    // Return true to stop parsing earlier.
    virtual bool OnXmlAbortParse(const CXmlElement& xmlElement) = 0;
};

// IWXmlElementHandler (wide-char string version)
// An application-specific class should implement this
// interface in order to handle XML events that are
// raised during parsing.
class IWXmlElementHandler
{
public:
    // Handle XML content events during parsing.
    virtual void OnXmlStartElement(const CWXmlElement& xmlElement) = 0;
    virtual void OnXmlElementData(const std::wstring& elementData, int depth) = 0;
    virtual void OnXmlEndElement(const CWXmlElement& xmlElement) = 0;
    
    // Handle XML error events during parsing.
    virtual void OnXmlError(int line, int column, const std::wstring& errorText, unsigned long errorCode) = 0;
    
    // Return true to stop parsing earlier.
    virtual bool OnXmlAbortParse(const CWXmlElement& xmlElement) = 0;
};

#endif // XML_ELEMENT_HANDLER_INCLUDED

// END

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions