Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Parsing XML using a C++ wrapper for SAX2

, 3 Aug 2005
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)

About the Author

nschan
Web Developer
Canada Canada
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 4 Aug 2005
Article Copyright 2005 by nschan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid