doc: updated bio man page with new cstore output mode
[barry.git] / src / xmlparser.cc
bloba58ebe027bb5779bbc4fd8f8b7e517015d04b93b
1 ///
2 /// \file xmlparser.cc
3 /// A simple XML parser (use callback on start, end and data block
4 ///
6 /*
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.
23 #include <iostream>
25 #include "xmlparser.h"
28 namespace Barry {
30 namespace XML {
33 XMLParser::XMLParser(std::istream& input, const char *charset)
34 : xmlpp::SaxParser()
35 , input(input)
37 this->charset = charset;
41 XMLParser::~XMLParser(void)
46 const unsigned long XMLParser::GetDepth(void) const
48 return depth;
52 bool XMLParser::Run(void)
54 try {
55 set_substitute_entities(true);
56 parse_chunk("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>");
58 std::string line;
59 while( getline(input, line) ) {
60 parse_chunk(line);
62 finish_chunk_parsing();
64 catch (const xmlpp::exception& ex) {
65 std::cout << "libxml++ exception: " << ex.what() << std::endl;
66 return false;
69 return true;
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;
89 depth++;
91 // Print attributes:
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;
103 depth--;
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;
137 } // namespace XML
139 } // namespace Barry