3 /// A simple XML parser (use callback on start, end and data block
7 Copyright (C) 2010, Nicolas VIVIEN
8 Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
25 #include "xmlparser.h"
33 XMLParser::XMLParser(std::istream
& input
, const char *charset
)
38 this->charset
= charset
;
42 XMLParser::~XMLParser(void)
47 const unsigned long XMLParser::GetDepth(void) const
53 bool XMLParser::Run(void)
56 set_substitute_entities(true);
57 parse_chunk("<?xml version=\"1.0\" encoding=\"" + charset
+ "\"?>");
60 while( getline(input
, line
) ) {
63 finish_chunk_parsing();
65 catch (const xmlpp::exception
& ex
) {
66 std::cout
<< "libxml++ exception: " << ex
.what() << std::endl
;
74 void XMLParser::on_start_document()
76 std::cout
<< "on_start_document()" << std::endl
;
80 void XMLParser::on_end_document()
82 std::cout
<< "on_end_document()" << std::endl
;
86 void XMLParser::on_start_element(const Glib::ustring
& name
,
87 const xmlpp::SaxParser::AttributeList
& attributes
)
89 std::cout
<< "Start:" << name
<< std::endl
;
93 for (xmlpp::SaxParser::AttributeList::const_iterator iter
= attributes
.begin(); iter
!= attributes
.end(); ++iter
) {
94 std::cout
<< " Attribute name=" << iter
->name
<< std::endl
;
96 std::cout
<< " , value= " << iter
->value
<< std::endl
;
101 void XMLParser::on_end_element(const Glib::ustring
& name
)
103 std::cout
<< "End:" << name
<< std::endl
;
108 void XMLParser::on_characters(const Glib::ustring
& text
)
110 std::cout
<< " Data:" << text
<< std::endl
;
114 void XMLParser::on_comment(const Glib::ustring
& text
)
116 std::cout
<< "on_comment(): " << text
<< std::endl
;
120 void XMLParser::on_warning(const Glib::ustring
& text
)
122 std::cout
<< "on_warning(): " << text
<< std::endl
;
126 void XMLParser::on_error(const Glib::ustring
& text
)
128 std::cout
<< "on_error(): " << text
<< std::endl
;
132 void XMLParser::on_fatal_error(const Glib::ustring
& text
)
134 std::cout
<< "on_fatal_error(): " << text
<< std::endl
;