reference/xml/examples.xml
b47e4bea197126359815c5e43403c4b77a0aaaa7
...
...
@@ -12,7 +12,7 @@
12
12
<programlisting role="php">
13
13
<![CDATA[
14
14
<?php
15
-
$file = "data.xml";
15
+
$file = "examples/book.xml";
16
16
$depth = 0;
17
17

18
18
function startElement($parser, $name, $attrs)
...
...
@@ -45,7 +45,6 @@ while ($data = fread($fp, 4096)) {
45
45
xml_get_current_line_number($xml_parser)));
46
46
}
47
47
}
48
-
xml_parser_free($xml_parser);
49
48
?>
50
49
]]>
51
50
</programlisting>
...
...
@@ -67,7 +66,7 @@ xml_parser_free($xml_parser);
67
66
<programlisting role="php">
68
67
<![CDATA[
69
68
<?php
70
-
$file = "data.xml";
69
+
$file = "examples/book.xml";
71
70
$map_array = array(
72
71
"BOLD" => "B",
73
72
"EMPHASIS" => "I",
...
...
@@ -111,7 +110,6 @@ while ($data = fread($fp, 4096)) {
111
110
xml_get_current_line_number($xml_parser)));
112
111
}
113
112
}
114
-
xml_parser_free($xml_parser);
115
113
?>
116
114
]]>
117
115
</programlisting>
...
...
@@ -135,7 +133,7 @@ xml_parser_free($xml_parser);
135
133
<para>
136
134
<example>
137
135
<title>External Entity Example</title>
138
-
<programlisting role="php">
136
+
<programlisting role="php" annotations="non-interactive">
139
137
<![CDATA[
140
138
<?php
141
139
$file = "xmltest.xml";
...
...
@@ -214,11 +212,9 @@ function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId,
214
212
printf("XML error: %s at line %d while parsing entity %s\n",
215
213
xml_error_string(xml_get_error_code($parser)),
216
214
xml_get_current_line_number($parser), $openEntityNames);
217
-
xml_parser_free($parser);
218
215
return false;
219
216
}
220
217
}
221
-
xml_parser_free($parser);
222
218
return true;
223
219
}
224
220
return false;
...
...
@@ -260,7 +256,6 @@ while ($data = fread($fp, 4096)) {
260
256
}
261
257
echo "</pre>";
262
258
echo "parse complete\n";
263
-
xml_parser_free($xml_parser);
264
259

265
260
?>
266
261
]]>
...
...
@@ -323,6 +318,86 @@ xml_parser_free($xml_parser);
323
318
</example>
324
319
</para>
325
320
</section>
321
+

322
+
<section xml:id="example.xml-parsing-with-class">
323
+
<title>XML Parsing With Class</title>
324
+
<para>
325
+
This example shows how to use a class with handlers.
326
+
<example>
327
+
<title>Show XML Element Structure</title>
328
+
<programlisting role="php">
329
+
<![CDATA[
330
+
<?php
331
+
$file = "examples/book.xml";
332
+

333
+
class CustomXMLParser
334
+
{
335
+
private $fp;
336
+
private $parser;
337
+
private $depth = 0;
338
+

339
+
function __construct(string $file)
340
+
{
341
+
if (!($this->fp = fopen($file, 'r'))) {
342
+
throw new RunTimeException("could not open XML file '{$file}'");
343
+
}
344
+

345
+
$this->parser = xml_parser_create();
346
+

347
+
xml_set_element_handler($this->parser, self::startElement(...), self::endElement(...));
348
+
xml_set_character_data_handler($this->parser, self::cdata(...));
349
+
}
350
+

351
+
private function startElement($parser, $name, $attrs)
352
+
{
353
+
for ($i = 0; $i < $this->depth; $i++) {
354
+
echo " ";
355
+
}
356
+
echo "$name\n";
357
+
$this->depth++;
358
+
}
359
+

360
+
private function endElement($parser, $name)
361
+
{
362
+
$this->depth--;
363
+
}
364
+

365
+
private function cdata($parse, $cdata)
366
+
{
367
+
if (trim($cdata) === '') {
368
+
return;
369
+
}
370
+
for ($i = 0; $i < $this->depth; $i++) {
371
+
echo " ";
372
+
}
373
+
echo trim($cdata), "\n";
374
+
}
375
+

376
+
public function parse()
377
+
{
378
+
while ($data = fread($this->fp, 4096)) {
379
+
if (!xml_parse($this->parser, $data, feof($this->fp))) {
380
+
throw new RunTimeException(
381
+
sprintf(
382
+
"XML error: %s at line %d",
383
+
xml_error_string(xml_get_error_code($this->parser)),
384
+
xml_get_current_line_number($this->parser)
385
+
)
386
+
);
387
+
}
388
+
}
389
+
}
390
+
}
391
+

392
+
$xmlParser = new CustomXMLParser($file);
393
+
$xmlParser->parse();
394
+
?>
395
+
]]>
396
+
</programlisting>
397
+
</example>
398
+
</para>
399
+
</section>
400
+

326
401
</chapter>
327
402

328
403
<!-- Keep this comment at the end of the file
...
...
@@ -345,4 +420,3 @@ vim600: syn=xml fen fdm=syntax fdl=2 si
345
420
vim: et tw=78 syn=sgml
346
421
vi: ts=1 sw=1
347
422
-->
348
-

349
423