|
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
|
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|
3 <html> |
|
4 <head> |
|
5 <title>LuaExpat: XML Expat parsing for the Lua programming language</title> |
|
6 <link rel="stylesheet" href="http://www.keplerproject.org/doc.css" type="text/css"/> |
|
7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> |
|
8 </head> |
|
9 <body> |
|
10 |
|
11 <div id="container"> |
|
12 |
|
13 <div id="product"> |
|
14 <div id="product_logo"><a href="http://www.keplerproject.org"> |
|
15 <img alt="LuaExpat logo" src="luaexpat.png"/> |
|
16 </a></div> |
|
17 <div id="product_name"><big><strong>LuaExpat</strong></big></div> |
|
18 <div id="product_description">XML Expat parsing for the Lua programming language</div> |
|
19 </div> <!-- id="product" --> |
|
20 |
|
21 <div id="main"> |
|
22 |
|
23 <div id="navigation"> |
|
24 <h1>LuaExpat</h1> |
|
25 <ul> |
|
26 <li><a href="index.html">Home</a> |
|
27 <ul> |
|
28 <li><a href="index.html#overview">Overview</a></li> |
|
29 <li><a href="index.html#status">Status</a></li> |
|
30 <li><a href="index.html#download">Download</a></li> |
|
31 <li><a href="index.html#history">History</a></li> |
|
32 <li><a href="index.html#references">References</a></li> |
|
33 <li><a href="index.html#credits">Credits</a></li> |
|
34 <li><a href="index.html#contact">Contact</a></li> |
|
35 </ul> |
|
36 </li> |
|
37 <li><a href="manual.html">Manual</a> |
|
38 <ul> |
|
39 <li><a href="manual.html#introduction">Introduction</a></li> |
|
40 <li><a href="manual.html#installation">Installation</a></li> |
|
41 <li><a href="manual.html#parser">Parser Objects</a></li> |
|
42 </ul> |
|
43 </li> |
|
44 <li><strong>Examples</strong></li> |
|
45 <li><a href="lom.html">Lua Object Model</a></li> |
|
46 <li><a href="http://luaforge.net/projects/luaexpat/">Project</a> |
|
47 <ul> |
|
48 <li><a href="http://luaforge.net/tracker/?group_id=13">Bug Tracker</a></li> |
|
49 <li><a href="http://luaforge.net/scm/?group_id=13">CVS</a></li> |
|
50 </ul> |
|
51 </li> |
|
52 <li><a href="license.html">License</a></li> |
|
53 </ul> |
|
54 </div> <!-- id="navigation" --> |
|
55 |
|
56 <div id="content"> |
|
57 |
|
58 <h2><a name="examples"></a>Examples</h2> |
|
59 |
|
60 <p>The code excerpt below creates a parser with 2 callbacks and |
|
61 feeds a test string to it. The parsing of the test string triggers |
|
62 the callbacks, printing the results.</p> |
|
63 |
|
64 <pre class="example"> |
|
65 require"lxp" |
|
66 |
|
67 local count = 0 |
|
68 callbacks = { |
|
69 StartElement = function (parser, name) |
|
70 io.write("+ ", string.rep(" ", count), name, "\n") |
|
71 count = count + 1 |
|
72 end, |
|
73 EndElement = function (parser, name) |
|
74 count = count - 1 |
|
75 io.write("- ", string.rep(" ", count), name, "\n") |
|
76 end |
|
77 } |
|
78 |
|
79 p = lxp.new(callbacks) |
|
80 |
|
81 for l in io.lines() do -- iterate lines |
|
82 p:parse(l) -- parses the line |
|
83 p:parse("\n") -- parses the end of line |
|
84 end |
|
85 p:parse() -- finishes the document |
|
86 p:close() -- closes the parser |
|
87 </pre> |
|
88 |
|
89 <p>For a test string like</p> |
|
90 |
|
91 <pre class="example"> |
|
92 <elem1> |
|
93 text |
|
94 <elem2/> |
|
95 more text |
|
96 </elem1> |
|
97 </pre> |
|
98 |
|
99 <p>The example would print</p> |
|
100 |
|
101 <pre class="example"> |
|
102 + elem1 |
|
103 + elem2 |
|
104 - elem2 |
|
105 - elem1 |
|
106 </pre> |
|
107 |
|
108 <p>Note that the text parts are not handled since the corresponding |
|
109 callback (<em>CharacterData</em>) has not been defined. Also note |
|
110 that defining this callback after the call to lxp.new would make no |
|
111 difference. But had the callback table been defined as</p> |
|
112 |
|
113 <pre class="example"> |
|
114 callbacks = { |
|
115 StartElement = function (parser, name) |
|
116 io.write("+ ", string.rep(" ", count), name, "\n") |
|
117 count = count + 1 |
|
118 end, |
|
119 EndElement = function (parser, name) |
|
120 count = count - 1 |
|
121 io.write("- ", string.rep(" ", count), name, "\n") |
|
122 end, |
|
123 CharacterData = function (parser, string) |
|
124 io.write("* ", string.rep(" ", count), string, "\n") |
|
125 end |
|
126 } |
|
127 </pre> |
|
128 |
|
129 <p>The results would have been</p> |
|
130 |
|
131 <pre class="example"> |
|
132 + elem1 |
|
133 * text |
|
134 + elem2 |
|
135 - elem2 |
|
136 * more text |
|
137 - elem1 |
|
138 </pre> |
|
139 |
|
140 <p>Another example would be the use of <em>false</em> as a |
|
141 placeholder for the callback. Suppose that we would like to print |
|
142 only the text associated with <em>elem2</em> elements and that the |
|
143 XML sample is</p> |
|
144 |
|
145 <pre class="example"> |
|
146 <elem1> |
|
147 text |
|
148 <elem2> |
|
149 inside text |
|
150 </elem2> |
|
151 more text |
|
152 </elem1> |
|
153 </pre> |
|
154 |
|
155 <p>We could define the new callback table as</p> |
|
156 |
|
157 <pre class="example"> |
|
158 callbacks = { |
|
159 StartElement = function (parser, name) |
|
160 if name == "elem2" then |
|
161 -- redefines CharacterData behaviour |
|
162 callbacks.CharacterData = function (parser, string) |
|
163 io.write(string, "\n") |
|
164 end |
|
165 end |
|
166 end, |
|
167 |
|
168 EndElement = function (parser, name) |
|
169 if name == "elem2" then |
|
170 callbacks.CharacterData = false -- restores placeholder |
|
171 end |
|
172 end, |
|
173 |
|
174 CharacterData = false -- placeholder |
|
175 } |
|
176 </pre> |
|
177 |
|
178 <p>The results would have been</p> |
|
179 |
|
180 <pre class="example"> |
|
181 inside text |
|
182 </pre> |
|
183 |
|
184 <p>Note that this example assumes no other elements are present |
|
185 inside elem2 tags.</p> |
|
186 |
|
187 </div> <!-- id="content" --> |
|
188 |
|
189 </div> <!-- id="main" --> |
|
190 |
|
191 <div id="about"> |
|
192 <p><a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" /></a></p> |
|
193 <p><small> |
|
194 $Id: examples.html,v 1.5 2006/03/23 00:19:37 carregal Exp $ |
|
195 </small></p> |
|
196 </div> <!-- id="about" --> |
|
197 |
|
198 </div> <!-- id="container" --> |
|
199 |
|
200 </body> |
|
201 </html> |