Click here to Skip to main content
15,891,204 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.7K   2.9K   77  
A basic C++ wrapper framework for the MSXML SAX2 API is presented.
// Filename: BookCatalog.h
// S.Chan, Jul 17, 2005

#ifndef BOOK_CATALOG_INCLUDED
#define BOOK_CATALOG_INCLUDED

// Include XML wrapper framework.
#include "XmlParser.h"

// STL includes.
#include <string>
#include <map>

// CBook
// Models a book in the catalog.
class CBook
{
public:
    CBook();
    ~CBook();

    enum
    {
        PROPERTY_NONE,
        PROPERTY_BOOKID,
        PROPERTY_AUTHOR,
        PROPERTY_TITLE,
        PROPERTY_GENRE,
        PROPERTY_PRICE,
        PROPERTY_PUBLISHDATE,
        PROPERTY_DESCRIPTION
    };

    const std::string& GetBookId() const { return m_bookId; }
    void  SetBookId(const std::string& bookId) { m_bookId = bookId; }

    const std::string& GetAuthor() const { return m_author; }
    void  SetAuthor(const std::string& author) { m_author = author; }

    const std::string& GetTitle() const { return m_title; }
    void  SetTitle(const std::string& title) { m_title = title; }

    const std::string& GetGenre() const { return m_genre; }
    void  SetGenre(const std::string& genre) { m_genre = genre; }

    const std::string& GetPrice() const { return m_price; }
    void  SetPrice(const std::string& price) { m_price = price; }

    const std::string& GetPublishDate() const { return m_publishDate; }
    void  SetPublishDate(const std::string& publishDate) { m_publishDate = publishDate; }

    const std::string& GetDescription() const { return m_description; }
    void  SetDescription(const std::string& description) { m_description = description; }

    std::string ToString() const;

private:
    std::string m_bookId;
    std::string m_author;
    std::string m_title;
    std::string m_genre;
    std::string m_price;
    std::string m_publishDate;
    std::string m_description;
};

// CBookCatalog
// Models a catalog of books.
class CBookCatalog : public IXmlElementHandler
{
public:
    CBookCatalog();
    ~CBookCatalog();

    // IXmlElementHandler virtual overrides.
    virtual void OnXmlStartElement(const CXmlElement& xmlElement);
    virtual void OnXmlElementData(const std::string& elementData, int depth);
    virtual void OnXmlEndElement(const CXmlElement& xmlElement);
    virtual void OnXmlError(int line, int column, const std::string& errorText, unsigned long errorCode);
    virtual bool OnXmlAbortParse(const CXmlElement& xmlElement);

    // Build the book catalog from an XML file.
    bool BuildFromXml(const std::string& xmlPath);

    // Catalog management.
    const CBook* FindBook(const std::string& bookId) const;
    int          GetBookCount() const;
    void         DeleteBooks();

private:
    // Used during parsing.
    bool         m_foundCatalog;
    CBook*       m_currBook;
    int          m_currBookProperty;

    // XML parser (wrapper class for SAX2).
    CXmlParser*  m_xmlParser;

    // Map keyed on Book ID.
    std::map<std::string,CBook*> m_books;
};
  
#endif // BOOK_CATALOG_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