ce023d4e7049efac7e65f09406ac2b3809a8035f
[platform/upstream/libxml++.git] / examples / sax_parser / myparser.cc
1 // -*- C++ -*-
2
3 /* myparser.cc
4  *
5  * Copyright (C) 2002 The libxml++ development team
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "myparser.h"
23 #include <glibmm/convert.h> //For Glib::ConvertError
24
25 #include <iostream>
26
27 MySaxParser::MySaxParser()
28   : xmlpp::SaxParser()
29 {
30 }
31
32 MySaxParser::~MySaxParser()
33 {
34 }
35
36 void MySaxParser::on_start_document()
37 {
38   std::cout << "on_start_document()" << std::endl;
39 }
40
41 void MySaxParser::on_end_document()
42 {
43   std::cout << "on_end_document()" << std::endl;
44 }
45
46 void MySaxParser::on_start_element(const Glib::ustring& name,
47                                    const AttributeList& attributes)
48 {
49   std::cout << "node name=" << name << std::endl;
50
51   // Print attributes:
52   for(xmlpp::SaxParser::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
53   {
54     try
55     {
56       std::cout << "  Attribute name=" << iter->name << std::endl;
57     }
58     catch(const Glib::ConvertError& ex)
59     {
60       std::cerr << "MySaxParser::on_start_element(): Exception caught while converting name for std::cout: " << ex.what() << std::endl;
61     }
62
63     try
64     {
65       std::cout << "    , value= " << iter->value << std::endl;
66     }
67     catch(const Glib::ConvertError& ex)
68     {
69       std::cerr << "MySaxParser::on_start_element(): Exception caught while converting value for std::cout: " << ex.what() << std::endl;
70     }
71   }
72 }
73
74 void MySaxParser::on_end_element(const Glib::ustring& /* name */)
75 {
76   std::cout << "on_end_element()" << std::endl;
77 }
78
79 void MySaxParser::on_characters(const Glib::ustring& text)
80 {
81   try
82   {
83     std::cout << "on_characters(): " << text << std::endl;
84   }
85   catch(const Glib::ConvertError& ex)
86   {
87     std::cerr << "MySaxParser::on_characters(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
88   }
89 }
90
91 void MySaxParser::on_comment(const Glib::ustring& text)
92 {
93   try
94   {
95     std::cout << "on_comment(): " << text << std::endl;
96   }
97   catch(const Glib::ConvertError& ex)
98   {
99     std::cerr << "MySaxParser::on_comment(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
100   }
101 }
102
103 void MySaxParser::on_warning(const Glib::ustring& text)
104 {
105   try
106   {
107     std::cout << "on_warning(): " << text << std::endl;
108   }
109   catch(const Glib::ConvertError& ex)
110   {
111     std::cerr << "MySaxParser::on_warning(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
112   }
113 }
114
115 void MySaxParser::on_error(const Glib::ustring& text)
116 {
117   try
118   {
119     std::cout << "on_error(): " << text << std::endl;
120   }
121   catch(const Glib::ConvertError& ex)
122   {
123     std::cerr << "MySaxParser::on_error(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
124   }
125 }
126
127 void MySaxParser::on_fatal_error(const Glib::ustring& text)
128 {
129   try
130   {
131     std::cout << "on_fatal_error(): " << text << std::endl;
132   }
133   catch(const Glib::ConvertError& ex)
134   {
135     std::cerr << "MySaxParser::on_characters(): Exception caught while converting value for std::cout: " << ex.what() << std::endl;
136   }
137 }
138