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: 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
{
public:
    CWXmlTester()
    {
        m_xmlParser = new CWXmlParser;
        m_xmlParser->AttachElementHandler(this);
    }

    ~CWXmlTester()
    {
        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");
        m_xmlParser->Parse(xmlPath);

        wprintf(L"\nParse 2...\n");
        m_xmlParser->RemoveValidationSchema(L"urn:books");
        m_xmlParser->Parse(xmlPath);
    }

    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;
    }

private:
    CWXmlParser* m_xmlParser;
};

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

    CWXmlTester xmlTester;
    xmlTester.Parse(L"..\\TestXmlSupport\\books2.xml");

	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.

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