# xmlparser.cc

Go to the documentation of this file.

    00001 ///
    00002 /// \file       xmlparser.cc
    00003 ///             A simple XML parser (use callback on start, end and data block
    00004 ///
    00005
    00006 /*
    00007     Copyright (C) 2010, Nicolas VIVIEN
    00008     Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
    00009
    00010     This program is free software; you can redistribute it and/or modify
    00011     it under the terms of the GNU General Public License as published by
    00012     the Free Software Foundation; either version 2 of the License, or
    00013     (at your option) any later version.
    00014
    00015     This program is distributed in the hope that it will be useful,
    00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00018
    00019     See the GNU General Public License in the COPYING file at the
    00020     root directory of this project for more details.
    00021 */
    00022
    00023 #include <iostream>
    00024
    00025 #include "xmlparser.h"
    00026
    00027
    00028 namespace Barry {
    00029
    00030 namespace XML {
    00031
    00032
    00033 XMLParser::XMLParser(std::istream& input, const char *charset)
    00034         : xmlpp::SaxParser()
    00035         , input(input)
    00036 {
    00037         this->depth = 0;
    00038         this->charset = charset;
    00039 }
    00040
    00041
    00042 XMLParser::~XMLParser(void)
    00043 {
    00044 }
    00045
    00046
    00047 const unsigned long XMLParser::GetDepth(void) const
    00048 {
    00049         return depth;
    00050 }
    00051
    00052
    00053 bool XMLParser::Run(void)
    00054 {
    00055         try {
    00056                 set_substitute_entities(true);
    00057                 parse_chunk("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>");
    00058
    00059                 std::string line;
    00060                 while( getline(input, line) ) {
    00061                         parse_chunk(line);
    00062                 }
    00063                 finish_chunk_parsing();
    00064         }
    00065         catch (const xmlpp::exception& ex) {
    00066                 std::cout << "libxml++ exception: " << ex.what() << std::endl;
    00067                 return false;
    00068         }
    00069
    00070         return true;
    00071 }
    00072
    00073
    00074 void XMLParser::on_start_document()
    00075 {
    00076         std::cout << "on_start_document()" << std::endl;
    00077 }
    00078
    00079
    00080 void XMLParser::on_end_document()
    00081 {
    00082         std::cout << "on_end_document()" << std::endl;
    00083 }
    00084
    00085
    00086 void XMLParser::on_start_element(const Glib::ustring& name,
    00087         const xmlpp::SaxParser::AttributeList& attributes)
    00088 {
    00089         std::cout << "Start:" << name << std::endl;
    00090         depth++;
    00091
    00092         // Print attributes:
    00093         for (xmlpp::SaxParser::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter) {
    00094                 std::cout << "  Attribute name=" << iter->name << std::endl;
    00095
    00096                 std::cout << "    , value= " << iter->value << std::endl;
    00097         }
    00098 }
    00099
    00100
    00101 void XMLParser::on_end_element(const Glib::ustring& name)
    00102 {
    00103         std::cout << "End:" << name << std::endl;
    00104         depth--;
    00105 }
    00106
    00107
    00108 void XMLParser::on_characters(const Glib::ustring& text)
    00109 {
    00110         std::cout << "  Data:" << text << std::endl;
    00111 }
    00112
    00113
    00114 void XMLParser::on_comment(const Glib::ustring& text)
    00115 {
    00116         std::cout << "on_comment(): " << text << std::endl;
    00117 }
    00118
    00119
    00120 void XMLParser::on_warning(const Glib::ustring& text)
    00121 {
    00122         std::cout << "on_warning(): " << text << std::endl;
    00123 }
    00124
    00125
    00126 void XMLParser::on_error(const Glib::ustring& text)
    00127 {
    00128         std::cout << "on_error(): " << text << std::endl;
    00129 }
    00130
    00131
    00132 void XMLParser::on_fatal_error(const Glib::ustring& text)
    00133 {
    00134         std::cout << "on_fatal_error(): " << text << std::endl;
    00135 }
    00136
    00137
    00138 } // namespace XML
    00139
    00140 } // namespace Barry
    00141