Imported Upstream version 2.91.2
[platform/upstream/libxml++.git] / docs / manual / html / ch02s03.html
1 <html>
2 <head>
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">
9 </head>
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>
14 <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>
19 </tr>
20 </table>
21 <hr>
22 </div>
23 <div class="sect1">
24 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
25 <a name="idp89476944"></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>
28 <div class="sect2">
29 <div class="titlepage"><div><div><h3 class="title">
30 <a name="idp89481584"></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>
33 <p>File: main.cc
34 </p>
35 <pre class="programlisting">
36 #ifdef HAVE_CONFIG_H
37 #include &lt;config.h&gt;
38 #endif
39
40 #include &lt;libxml++/libxml++.h&gt;
41 #include &lt;libxml++/parsers/textreader.h&gt;
42
43 #include &lt;iostream&gt;
44 #include &lt;stdlib.h&gt;
45
46 struct indent {
47   int depth_;
48   indent(int depth): depth_(depth) {};
49 };
50
51 std::ostream &amp; operator&lt;&lt;(std::ostream &amp; o, indent const &amp; in)
52 {
53   for(int i = 0; i != in.depth_; ++i)
54   {
55     o &lt;&lt; "  ";
56   }
57   return o;
58 }
59
60 int main(int /* argc */, char** /* argv */)
61 {
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(""));
65
66   try
67   {
68     xmlpp::TextReader reader("example.xml");
69
70     while(reader.read())
71     {
72       int depth = reader.get_depth();
73       std::cout &lt;&lt; indent(depth) &lt;&lt; "--- node ---" &lt;&lt; std::endl;
74       std::cout &lt;&lt; indent(depth) &lt;&lt; "name: " &lt;&lt; reader.get_name() &lt;&lt; std::endl;
75       std::cout &lt;&lt; indent(depth) &lt;&lt; "depth: " &lt;&lt; reader.get_depth() &lt;&lt; std::endl;
76
77       if(reader.has_attributes())
78       {
79         std::cout &lt;&lt; indent(depth) &lt;&lt; "attributes: " &lt;&lt; std::endl;
80         reader.move_to_first_attribute();
81         do
82         {
83           std::cout &lt;&lt; indent(depth) &lt;&lt; "  " &lt;&lt; reader.get_name() &lt;&lt; ": " &lt;&lt; reader.get_value() &lt;&lt; std::endl;
84         } while(reader.move_to_next_attribute());
85         reader.move_to_element();
86       }
87       else
88       {
89         std::cout &lt;&lt; indent(depth) &lt;&lt; "no attributes" &lt;&lt; std::endl;
90       }
91
92       if(reader.has_value())
93         std::cout &lt;&lt; indent(depth) &lt;&lt; "value: '" &lt;&lt; reader.get_value() &lt;&lt; "'" &lt;&lt; std::endl;
94       else
95         std::cout &lt;&lt; indent(depth) &lt;&lt; "novalue" &lt;&lt; std::endl;
96
97     }
98   }
99   catch(const std::exception&amp; e)
100   {
101     std::cerr &lt;&lt; "Exception caught: " &lt;&lt; e.what() &lt;&lt; std::endl;
102     return EXIT_FAILURE;
103   }
104   return EXIT_SUCCESS;
105 }
106
107 </pre>
108 <p>
109 </p>
110 </div>
111 </div>
112 <div class="navfooter">
113 <hr>
114 <table width="100%" summary="Navigation footer">
115 <tr>
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>
120 </tr>
121 <tr>
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>
125 </tr>
126 </table>
127 </div>
128 </body>
129 </html>