@@ -74,23 +74,23 @@ $map_array = array(
74
74
"LITERAL" => "TT"
75
75
);
76
76
77
- function startElement($parser, $name, $attrs)
77
+ function startElement($parser, $name, $attrs)
78
78
{
79
79
global $map_array;
80
80
if (isset($map_array[$name])) {
81
81
echo "<$map_array[$name]>";
82
82
}
83
83
}
84
84
85
- function endElement($parser, $name)
85
+ function endElement($parser, $name)
86
86
{
87
87
global $map_array;
88
88
if (isset($map_array[$name])) {
89
89
echo "</$map_array[$name]>";
90
90
}
91
91
}
92
92
93
- function characterData($parser, $data)
93
+ function characterData($parser, $data)
94
94
{
95
95
echo $data;
96
96
}
@@ -140,39 +140,39 @@ xml_parser_free($xml_parser);
140
140
<?php
141
141
$file = "xmltest.xml";
142
142
143
- function trustedFile($file)
143
+ function trustedFile($file)
144
144
{
145
145
// only trust local files owned by ourselves
146
- if (!preg_match("@^([a-z][a-z0-9+.-]*)\:\/\/@i", $file)
146
+ if (!preg_match("@^([a-z][a-z0-9+.-]*)\:\/\/@i", $file)
147
147
&& fileowner($file) == getmyuid()) {
148
148
return true;
149
149
}
150
150
return false;
151
151
}
152
152
153
- function startElement($parser, $name, $attribs)
153
+ function startElement($parser, $name, $attribs)
154
154
{
155
155
echo "<<font color=\"#0000cc\">$name</font>";
156
156
if (count($attribs)) {
157
157
foreach ($attribs as $k => $v) {
158
- echo " <font color=\"#009900\">$k</font>=\"<font
158
+ echo " <font color=\"#009900\">$k</font>=\"<font
159
159
color=\"#990000\">$v</font>\"";
160
160
}
161
161
}
162
162
echo ">";
163
163
}
164
164
165
- function endElement($parser, $name)
165
+ function endElement($parser, $name)
166
166
{
167
167
echo "</<font color=\"#0000cc\">$name</font>>";
168
168
}
169
169
170
- function characterData($parser, $data)
170
+ function characterData($parser, $data)
171
171
{
172
172
echo "<b>$data</b>";
173
173
}
174
174
175
- function PIHandler($parser, $target, $data)
175
+ function PIHandler($parser, $target, $data)
176
176
{
177
177
switch (strtolower($target)) {
178
178
case "php":
@@ -183,20 +183,20 @@ function PIHandler($parser, $target, $data)
183
183
if (trustedFile($parser_file[$parser])) {
184
184
eval($data);
185
185
} else {
186
- printf("Untrusted PHP code: <i>%s</i>",
186
+ printf("Untrusted PHP code: <i>%s</i>",
187
187
htmlspecialchars($data));
188
188
}
189
189
break;
190
190
}
191
191
}
192
192
193
- function defaultHandler($parser, $data)
193
+ function defaultHandler($parser, $data)
194
194
{
195
195
if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
196
- printf('<font color="#aa00aa">%s</font>',
196
+ printf('<font color="#aa00aa">%s</font>',
197
197
htmlspecialchars($data));
198
198
} else {
199
- printf('<font size="-1">%s</font>',
199
+ printf('<font size="-1">%s</font>',
200
200
htmlspecialchars($data));
201
201
}
202
202
}
@@ -224,7 +224,7 @@ function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId,
224
224
return false;
225
225
}
226
226
227
- function new_xml_parser($file)
227
+ function new_xml_parser($file)
228
228
{
229
229
global $parser_file;
230
230
@@ -235,7 +235,7 @@ function new_xml_parser($file)
235
235
xml_set_processing_instruction_handler($xml_parser, "PIHandler");
236
236
xml_set_default_handler($xml_parser, "defaultHandler");
237
237
xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");
238
-
238
+
239
239
if (!($fp = @fopen($file, "r"))) {
240
240
return false;
241
241
}
@@ -323,6 +323,86 @@ xml_parser_free($xml_parser);
323
323
</example >
324
324
</para >
325
325
</section >
326
+
327
+ <section xml : id =" example.xml-parsing-with-class" >
328
+ <title >XML Parsing With Class</title >
329
+ <para >
330
+ This example shows how to use a class with handlers.
331
+ <example >
332
+ <title >Show XML Element Structure</title >
333
+ <programlisting role =" php" >
334
+ <![CDATA[
335
+ <?php
336
+ $file = "examples/book.xml";
337
+
338
+ class CustomXMLParser
339
+ {
340
+ private $fp;
341
+ private $parser;
342
+ private $depth = 0;
343
+
344
+ function __construct(string $file)
345
+ {
346
+ if (!($this->fp = fopen($file, 'r'))) {
347
+ throw new RunTimeException("could not open XML file '{$file}'");
348
+ }
349
+
350
+ $this->parser = xml_parser_create();
351
+
352
+ xml_set_element_handler($this->parser, self::startElement(...), self::endElement(...));
353
+ xml_set_character_data_handler($this->parser, self::cdata(...));
354
+ }
355
+
356
+ private function startElement($parser, $name, $attrs)
357
+ {
358
+ for ($i = 0; $i < $this->depth; $i++) {
359
+ echo " ";
360
+ }
361
+ echo "$name\n";
362
+ $this->depth++;
363
+ }
364
+
365
+ private function endElement($parser, $name)
366
+ {
367
+ $this->depth--;
368
+ }
369
+
370
+ private function cdata($parse, $cdata)
371
+ {
372
+ if (trim($cdata) === '') {
373
+ return;
374
+ }
375
+ for ($i = 0; $i < $this->depth; $i++) {
376
+ echo " ";
377
+ }
378
+ echo trim($cdata), "\n";
379
+ }
380
+
381
+ public function parse()
382
+ {
383
+ while ($data = fread($this->fp, 4096)) {
384
+ if (!xml_parse($this->parser, $data, feof($this->fp))) {
385
+ throw new RunTimeException(
386
+ sprintf(
387
+ "XML error: %s at line %d",
388
+ xml_error_string(xml_get_error_code($this->parser)),
389
+ xml_get_current_line_number($this->parser)
390
+ )
391
+ );
392
+ }
393
+ }
394
+ }
395
+ }
396
+
397
+ $xmlParser = new CustomXMLParser($file);
398
+ $xmlParser->parse();
399
+ ?>
400
+ ]]>
401
+ </programlisting >
402
+ </example >
403
+ </para >
404
+ </section >
405
+
326
406
</chapter >
327
407
328
408
<!-- Keep this comment at the end of the file
0 commit comments