3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>TextReader 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="ch02s02.html" title="SAX Parser">
10 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
11 <div class="navheader">
12 <table width="100%" summary="Navigation header">
13 <tr><th colspan="3" align="center">TextReader Parser</th></tr>
15 <td width="20%" align="left">
16 <a accesskey="p" href="ch02s02.html">Prev</a> </td>
17 <th width="60%" align="center">Chapter 2. Parsers</th>
18 <td width="20%" align="right"> </td>
24 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
25 <a name="idp89483584"></a>TextReader Parser</h2></div></div></div>
26 <p>Like the SAX parser, the TextReader parser is suitable for sequential parsing, but instead of implementing handlers for specific parts of the document, it allows you to detect the current node type, process the node accordingly, and skip forward in the document as much as necessary. Unlike the DOM parser, you may not move backwards in the XML document. And unlike the SAX parser, you must not waste time processing nodes that do not interest you. </p>
27 <p>All methods are on the single parser instance, but their result depends on the current context. For instance, use <code class="literal">read()</code> to move to the next node, and <code class="literal">move_to_element()</code> to navigate to child nodes. These methods will return false when no more nodes are available. Then use methods such as <code class="literal">get_name()</code> and <code class="literal">get_value()</code> to examine the elements and their attributes.</p>
29 <div class="titlepage"><div><div><h3 class="title">
30 <a name="idp89488224"></a>Example</h3></div></div></div>
31 <p>This example examines each node in turn, then moves to the next node.</p>
32 <p><a class="ulink" href="http://git.gnome.org/browse/libxml++/tree/examples/textreader" target="_top">Source Code</a></p>
35 <pre class="programlisting">
37 #include <config.h>
40 #include <libxml++/libxml++.h>
41 #include <libxml++/parsers/textreader.h>
43 #include <iostream>
44 #include <stdlib.h>
48 indent(int depth): depth_(depth) {};
51 std::ostream & operator<<(std::ostream & o, indent const & in)
53 for(int i = 0; i != in.depth_; ++i)
60 int main(int /* argc */, char** /* argv */)
62 // Set the global C and C++ locale to the user-configured locale,
63 // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
64 std::locale::global(std::locale(""));
68 xmlpp::TextReader reader("example.xml");
72 int depth = reader.get_depth();
73 std::cout << indent(depth) << "--- node ---" << std::endl;
74 std::cout << indent(depth) << "name: " << reader.get_name() << std::endl;
75 std::cout << indent(depth) << "depth: " << reader.get_depth() << std::endl;
77 if(reader.has_attributes())
79 std::cout << indent(depth) << "attributes: " << std::endl;
80 reader.move_to_first_attribute();
83 std::cout << indent(depth) << " " << reader.get_name() << ": " << reader.get_value() << std::endl;
84 } while(reader.move_to_next_attribute());
85 reader.move_to_element();
89 std::cout << indent(depth) << "no attributes" << std::endl;
92 if(reader.has_value())
93 std::cout << indent(depth) << "value: '" << reader.get_value() << "'" << std::endl;
95 std::cout << indent(depth) << "novalue" << std::endl;
99 catch(const std::exception& e)
101 std::cerr << "Exception caught: " << e.what() << std::endl;
112 <div class="navfooter">
114 <table width="100%" summary="Navigation footer">
116 <td width="40%" align="left">
117 <a accesskey="p" href="ch02s02.html">Prev</a> </td>
118 <td width="20%" align="center"><a accesskey="u" href="chapter-parsers.html">Up</a></td>
119 <td width="40%" align="right"> </td>
122 <td width="40%" align="left" valign="top">SAX Parser </td>
123 <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
124 <td width="40%" align="right" valign="top"> </td>