3 /// A simple XML parser (use callback on start, end and data block
7 Copyright (C) 2010, Nicolas VIVIEN
8 Copyright (C) 2005-2010, 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
)
37 this->charset
= charset
;
41 XMLParser::~XMLParser(void)
46 const unsigned long XMLParser::GetDepth(void) const
52 bool XMLParser::Run(void)
55 set_substitute_entities(true);
56 parse_chunk("<?xml version=\"1.0\" encoding=\"" + charset
+ "\"?>");
59 while( getline(input
, line
) ) {
62 finish_chunk_parsing();
64 catch (const xmlpp::exception
& ex
) {
65 std::cout
<< "libxml++ exception: " << ex
.what() << std::endl
;
73 void XMLParser::on_start_document()
75 std::cout
<< "on_start_document()" << std::endl
;
79 void XMLParser::on_end_document()
81 std::cout
<< "on_end_document()" << std::endl
;
85 void XMLParser::on_start_element(const Glib::ustring
& name
,
86 const xmlpp::SaxParser::AttributeList
& attributes
)
88 std::cout
<< "Start:" << name
<< std::endl
;
92 for (xmlpp::SaxParser::AttributeList::const_iterator iter
= attributes
.begin(); iter
!= attributes
.end(); ++iter
) {
93 std::cout
<< " Attribute name=" << iter
->name
<< std::endl
;
95 std::cout
<< " , value= " << iter
->value
<< std::endl
;
100 void XMLParser::on_end_element(const Glib::ustring
& name
)
102 std::cout
<< "End:" << name
<< std::endl
;
107 void XMLParser::on_characters(const Glib::ustring
& text
)
109 std::cout
<< " Data:" << text
<< std::endl
;
113 void XMLParser::on_comment(const Glib::ustring
& text
)
115 std::cout
<< "on_comment(): " << text
<< std::endl
;
119 void XMLParser::on_warning(const Glib::ustring
& text
)
121 std::cout
<< "on_warning(): " << text
<< std::endl
;
125 void XMLParser::on_error(const Glib::ustring
& text
)
127 std::cout
<< "on_error(): " << text
<< std::endl
;
131 void XMLParser::on_fatal_error(const Glib::ustring
& text
)
133 std::cout
<< "on_fatal_error(): " << text
<< std::endl
;