3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>SAX Parser</title>
5 <meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
6 <link rel="home" href="index.html" title="libxml++ - An XML Parser for C++">
7 <link rel="up" href="chapter-parsers.html" title="Chapter 2. Parsers">
8 <link rel="prev" href="chapter-parsers.html" title="Chapter 2. Parsers">
9 <link rel="next" href="ch02s03.html" title="TextReader Parser">
11 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
12 <div class="navheader">
13 <table width="100%" summary="Navigation header">
14 <tr><th colspan="3" align="center">SAX Parser</th></tr>
16 <td width="20%" align="left">
17 <a accesskey="p" href="chapter-parsers.html">Prev</a> </td>
18 <th width="60%" align="center">Chapter 2. Parsers</th>
19 <td width="20%" align="right"> <a accesskey="n" href="ch02s03.html">Next</a>
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="idp89461168"></a>SAX Parser</h2></div></div></div>
28 <p>The SAX (Simple API for XML) parser presents each node of the XML document in sequence. So when you process one node, you must have already stored information about any relevant previous nodes, and you have no information at that time about subsequent nodes. The SAX parser uses less memory than the DOM parser and it is a suitable abstraction for documents that can be processed sequentially rather than as a whole.</p>
29 <p>By using the <code class="literal">parse_chunk()</code> method instead of <code class="literal">parse()</code>, you can even parse parts of the XML document before you have received the whole document.</p>
30 <p>As shown in the example, you should derive your own class from SaxParser and override some of the virtual methods. These "handler" methods will be called while the document is parsed.</p>
32 <div class="titlepage"><div><div><h3 class="title">
33 <a name="idp89465008"></a>Example</h3></div></div></div>
34 <p>This example shows how the handler methods are called during parsing.</p>
35 <p><a class="ulink" href="http://git.gnome.org/browse/libxml++/tree/examples/sax_parser" target="_top">Source Code</a></p>
38 <pre class="programlisting">
39 #ifndef __LIBXMLPP_EXAMPLES_MYPARSER_H
40 #define __LIBXMLPP_EXAMPLES_MYPARSER_H
42 #include <libxml++/libxml++.h>
44 class MySaxParser : public xmlpp::SaxParser
48 ~MySaxParser() override;
52 void on_start_document() override;
53 void on_end_document() override;
54 void on_start_element(const Glib::ustring& name,
55 const AttributeList& properties) override;
56 void on_end_element(const Glib::ustring& name) override;
57 void on_characters(const Glib::ustring& characters) override;
58 void on_comment(const Glib::ustring& text) override;
59 void on_warning(const Glib::ustring& text) override;
60 void on_error(const Glib::ustring& text) override;
61 void on_fatal_error(const Glib::ustring& text) override;
65 #endif //__LIBXMLPP_EXAMPLES_MYPARSER_H
71 <pre class="programlisting">
73 #include <config.h>
76 #include <fstream>
77 #include <iostream>
78 #include <stdlib.h>
79 #include <cstring> // std::memset()
84 main(int argc, char* argv[])
86 // Set the global C and C++ locale to the user-configured locale,
87 // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
88 std::locale::global(std::locale(""));
92 filepath = argv[1]; //Allow the user to specify a different XML file to parse.
94 filepath = "example.xml";
96 // Parse the entire document in one go:
97 auto return_code = EXIT_SUCCESS;
101 parser.set_substitute_entities(true);
102 parser.parse_file(filepath);
104 catch(const xmlpp::exception& ex)
106 std::cerr << "libxml++ exception: " << ex.what() << std::endl;
107 return_code = EXIT_FAILURE;
110 // Incremental parsing, sometimes useful for network connections:
113 std::cout << std::endl << "Incremental SAX Parser:" << std::endl;
115 std::ifstream is(filepath.c_str());
117 throw xmlpp::exception("Could not open file " + filepath);
120 const size_t buffer_size = sizeof(buffer) / sizeof(char);
124 parser.set_substitute_entities(true);
127 std::memset(buffer, 0, buffer_size);
128 is.read(buffer, buffer_size-1);
131 // We use Glib::ustring::ustring(InputIterator begin, InputIterator end)
132 // instead of Glib::ustring::ustring( const char*, size_type ) because it
133 // expects the length of the string in characters, not in bytes.
134 Glib::ustring input(buffer, buffer+is.gcount());
135 parser.parse_chunk(input);
140 parser.finish_chunk_parsing();
142 catch(const xmlpp::exception& ex)
144 std::cerr << "Incremental parsing, libxml++ exception: " << ex.what() << std::endl;
145 return_code = EXIT_FAILURE;
156 <pre class="programlisting">
157 #include "myparser.h"
158 #include <glibmm/convert.h> //For Glib::ConvertError
160 #include <iostream>
162 MySaxParser::MySaxParser()
167 MySaxParser::~MySaxParser()
171 void MySaxParser::on_start_document()
173 std::cout << "on_start_document()" << std::endl;
176 void MySaxParser::on_end_document()
178 std::cout << "on_end_document()" << std::endl;
181 void MySaxParser::on_start_element(const Glib::ustring& name,
182 const AttributeList& attributes)
184 std::cout << "node name=" << name << std::endl;
187 for(const auto& attr_pair : attributes)
191 std::cout << " Attribute name=" << attr_pair.name << std::endl;
193 catch(const Glib::ConvertError& ex)
195 std::cerr << "MySaxParser::on_start_element(): Exception caught while converting name for std::cout: " << ex.what() << std::endl;
200 std::cout << " , value= " << attr_pair.value << std::endl;
202 catch(const Glib::ConvertError& ex)
204 std::cerr << "MySaxParser::on_start_element(): Exception caught while converting value for std::cout: " << ex.what() << std::endl;
209 void MySaxParser::on_end_element(const Glib::ustring& /* name */)
211 std::cout << "on_end_element()" << std::endl;
214 void MySaxParser::on_characters(const Glib::ustring& text)
218 std::cout << "on_characters(): " << text << std::endl;
220 catch(const Glib::ConvertError& ex)
222 std::cerr << "MySaxParser::on_characters(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
226 void MySaxParser::on_comment(const Glib::ustring& text)
230 std::cout << "on_comment(): " << text << std::endl;
232 catch(const Glib::ConvertError& ex)
234 std::cerr << "MySaxParser::on_comment(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
238 void MySaxParser::on_warning(const Glib::ustring& text)
242 std::cout << "on_warning(): " << text << std::endl;
244 catch(const Glib::ConvertError& ex)
246 std::cerr << "MySaxParser::on_warning(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
250 void MySaxParser::on_error(const Glib::ustring& text)
254 std::cout << "on_error(): " << text << std::endl;
256 catch(const Glib::ConvertError& ex)
258 std::cerr << "MySaxParser::on_error(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
262 void MySaxParser::on_fatal_error(const Glib::ustring& text)
266 std::cout << "on_fatal_error(): " << text << std::endl;
268 catch(const Glib::ConvertError& ex)
270 std::cerr << "MySaxParser::on_characters(): Exception caught while converting value for std::cout: " << ex.what() << std::endl;
279 <div class="navfooter">
281 <table width="100%" summary="Navigation footer">
283 <td width="40%" align="left">
284 <a accesskey="p" href="chapter-parsers.html">Prev</a> </td>
285 <td width="20%" align="center"><a accesskey="u" href="chapter-parsers.html">Up</a></td>
286 <td width="40%" align="right"> <a accesskey="n" href="ch02s03.html">Next</a>
290 <td width="40%" align="left" valign="top">Chapter 2. Parsers </td>
291 <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
292 <td width="40%" align="right" valign="top"> TextReader Parser</td>