Click here to Skip to main content
12,071,437 members (33,659 online)
Click here to Skip to main content

Stats

124.7K views
2.4K downloads
76 bookmarked
Posted

Parsing XML using a C++ wrapper for SAX2

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

Share

About the Author

nschan
Web Developer
Canada Canada
No Biography provided

You may also be interested in...

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