Click here to Skip to main content
11,644,286 members (69,784 online)
Click here to Skip to main content
Add your own
alternative version

Parsing XML using a C++ wrapper for SAX2

, 3 Aug 2005 CPOL 120.4K 2.3K 76
A basic C++ wrapper framework for the MSXML SAX2 API is presented.
// Filename: TestConsole.cpp
// 2005-07-29 nschan Initial revision.

// Simple console app to test the CWXmlParser wrapper class
// that supports wide-char strings.

#include "stdafx.h"
#include "XmlParser.h"
#include <windows.h>
#include <comdef.h>

class CWXmlTester : public IWXmlElementHandler
        m_xmlParser = new CWXmlParser;

        delete m_xmlParser;

    void Parse(const std::wstring& xmlPath)
        m_xmlParser->SetParserFeature(L"schema-validation", true);
        m_xmlParser->SetParserFeature(L"use-schema-location", false);

        wprintf(L"\nParse 1...\n");
        m_xmlParser->AddValidationSchema(L"urn:books", L"..\\TestXmlSupport\\books2.xsd");

        wprintf(L"\nParse 2...\n");

    virtual void OnXmlStartElement(const CWXmlElement& xmlElement)
        wprintf(L"%s\n", xmlElement.ToString().c_str());
    virtual void OnXmlElementData(const std::wstring& elementData, int depth)
        for(int i = 0; i < depth; ++i)
            wprintf(L"    ");
        wprintf(L"%s\n", elementData.c_str());
    virtual void OnXmlEndElement(const CWXmlElement& xmlElement)
        wprintf(L"%s\n", xmlElement.ToString().c_str());
    virtual void OnXmlError(int line, int column, const std::wstring& errorText, unsigned long errorCode)
        wprintf(L"line %d, column %d: %s, errorCode=%d\n",
                line, column, errorText.c_str(), errorCode);
    virtual bool OnXmlAbortParse(const CWXmlElement& xmlElement)
        // Uncomment this if you want to test abort.
        // if ( xmlElement.GetAttributeValue(L"id") == L"bk002" )
        //     return true;

        return false;

    CWXmlParser* m_xmlParser;

int main(int argc, char* argv[])
	printf("Hello World!\n");

    CWXmlTester xmlTester;

	return 0;

// 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.


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


About the Author

Web Developer
Canada Canada
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 4 Aug 2005
Article Copyright 2005 by nschan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid