Skip to content

Commit 802374d

Browse files
committed
Add new XML parser example that uses a class
1 parent c1f37a6 commit 802374d

File tree

1 file changed

+96
-16
lines changed

1 file changed

+96
-16
lines changed

reference/xml/examples.xml

Lines changed: 96 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,23 @@ $map_array = array(
7474
"LITERAL" => "TT"
7575
);
7676
77-
function startElement($parser, $name, $attrs)
77+
function startElement($parser, $name, $attrs)
7878
{
7979
global $map_array;
8080
if (isset($map_array[$name])) {
8181
echo "<$map_array[$name]>";
8282
}
8383
}
8484
85-
function endElement($parser, $name)
85+
function endElement($parser, $name)
8686
{
8787
global $map_array;
8888
if (isset($map_array[$name])) {
8989
echo "</$map_array[$name]>";
9090
}
9191
}
9292
93-
function characterData($parser, $data)
93+
function characterData($parser, $data)
9494
{
9595
echo $data;
9696
}
@@ -140,39 +140,39 @@ xml_parser_free($xml_parser);
140140
<?php
141141
$file = "xmltest.xml";
142142
143-
function trustedFile($file)
143+
function trustedFile($file)
144144
{
145145
// 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)
147147
&& fileowner($file) == getmyuid()) {
148148
return true;
149149
}
150150
return false;
151151
}
152152
153-
function startElement($parser, $name, $attribs)
153+
function startElement($parser, $name, $attribs)
154154
{
155155
echo "&lt;<font color=\"#0000cc\">$name</font>";
156156
if (count($attribs)) {
157157
foreach ($attribs as $k => $v) {
158-
echo " <font color=\"#009900\">$k</font>=\"<font
158+
echo " <font color=\"#009900\">$k</font>=\"<font
159159
color=\"#990000\">$v</font>\"";
160160
}
161161
}
162162
echo "&gt;";
163163
}
164164
165-
function endElement($parser, $name)
165+
function endElement($parser, $name)
166166
{
167167
echo "&lt;/<font color=\"#0000cc\">$name</font>&gt;";
168168
}
169169
170-
function characterData($parser, $data)
170+
function characterData($parser, $data)
171171
{
172172
echo "<b>$data</b>";
173173
}
174174
175-
function PIHandler($parser, $target, $data)
175+
function PIHandler($parser, $target, $data)
176176
{
177177
switch (strtolower($target)) {
178178
case "php":
@@ -183,20 +183,20 @@ function PIHandler($parser, $target, $data)
183183
if (trustedFile($parser_file[$parser])) {
184184
eval($data);
185185
} else {
186-
printf("Untrusted PHP code: <i>%s</i>",
186+
printf("Untrusted PHP code: <i>%s</i>",
187187
htmlspecialchars($data));
188188
}
189189
break;
190190
}
191191
}
192192
193-
function defaultHandler($parser, $data)
193+
function defaultHandler($parser, $data)
194194
{
195195
if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
196-
printf('<font color="#aa00aa">%s</font>',
196+
printf('<font color="#aa00aa">%s</font>',
197197
htmlspecialchars($data));
198198
} else {
199-
printf('<font size="-1">%s</font>',
199+
printf('<font size="-1">%s</font>',
200200
htmlspecialchars($data));
201201
}
202202
}
@@ -224,7 +224,7 @@ function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId,
224224
return false;
225225
}
226226
227-
function new_xml_parser($file)
227+
function new_xml_parser($file)
228228
{
229229
global $parser_file;
230230
@@ -235,7 +235,7 @@ function new_xml_parser($file)
235235
xml_set_processing_instruction_handler($xml_parser, "PIHandler");
236236
xml_set_default_handler($xml_parser, "defaultHandler");
237237
xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");
238-
238+
239239
if (!($fp = @fopen($file, "r"))) {
240240
return false;
241241
}
@@ -323,6 +323,86 @@ xml_parser_free($xml_parser);
323323
</example>
324324
</para>
325325
</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+
326406
</chapter>
327407

328408
<!-- Keep this comment at the end of the file

0 commit comments

Comments
 (0)