xref: /PHP-8.3/ext/xml/tests/xml003.phpt (revision 8567bc10)
1--TEST--
2XML parser test, xml_set_object callbacks
3--EXTENSIONS--
4xml
5--SKIPIF--
6<?php
7XML_SAX_IMPL == 'libxml' && die('skip this test is not intended for libxml SAX parser');
8?>
9--FILE--
10<?php
11chdir(__DIR__);
12
13class myclass
14{
15    function startElement($parser, $name, $attribs)
16    {
17        print '{'.$name;
18        if (sizeof($attribs)) {
19            foreach ($attribs as $k => $v) {
20                print " $k=\"$v\"";
21            }
22        }
23        print '}';
24    }
25    function endElement($parser, $name)
26    {
27        print '{/'.$name.'}';
28    }
29    function characterData($parser, $data)
30    {
31        print '{CDATA['.$data.']}';
32    }
33    function PIHandler($parser, $target, $data)
34    {
35        print '{PI['.$target.','.$data.']}';
36    }
37    function defaultHandler($parser, $data)
38    {
39        if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
40            print '{ENTREF['.$data.']}';
41        } else {
42            print '{?['.$data.']}';
43        }
44    }
45    function externalEntityRefHandler($parser, $openEntityNames, $base,	$systemId, $publicId)
46    {
47        print '{EXTENTREF['.$openEntityNames.','.$base.','.$systemId.','.$publicId."]}\n";
48        return true;
49    }
50}
51
52$xml_parser = xml_parser_create();
53$obj = new myclass;
54xml_set_object($xml_parser, $obj);
55xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
56xml_set_element_handler($xml_parser, "startElement", "endElement");
57xml_set_character_data_handler($xml_parser, "characterData");
58xml_set_processing_instruction_handler($xml_parser, "PIHandler");
59xml_set_default_handler($xml_parser, "defaultHandler");
60xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");
61
62if (!($fp = @fopen("xmltest.xml", "r"))) {
63    die("could not open XML input");
64}
65
66while ($data = fread($fp, 4096)) {
67    if (!xml_parse($xml_parser, $data, feof($fp))) {
68        die(sprintf("XML error: %s at line %d\n",
69        xml_error_string(xml_get_error_code($xml_parser)),
70        xml_get_current_line_number($xml_parser)));
71    }
72}
73print "parse complete\n";
74xml_parser_free($xml_parser);
75?>
76--EXPECT--
77{?[<?xml version="1.0" encoding="ISO-8859-1"?>]}{?[
78]}{?[<!DOCTYPE]}{?[ ]}{?[phptest]}{?[ ]}{?[SYSTEM]}{?[ ]}{?["notfound.dtd"]}{?[ ]}{?[[]}{?[
79]}{?[<!ENTITY]}{?[ ]}{?[%]}{?[ ]}{?[incent]}{?[ ]}{?[SYSTEM]}{?[ ]}{?["inc.ent"]}{?[>]}{?[
80]}{?[%incent;]}{?[
81]}{?[]]}{?[>]}{?[
82]}{ROOT ID="elem1"}{CDATA[
83]}{CDATA[ Plain text.]}{CDATA[
84]}{CDATA[ ]}{ELEM1}{CDATA[
85]}{CDATA[  ]}{?[<!-- comment -->]}{CDATA[
86]}{CDATA[  ]}{ELEM2}{CDATA[
87]}{CDATA[   ]}{?[<![CDATA[]}{CDATA[CDATA block]}{?[]]>]}{CDATA[
88]}{CDATA[   ]}{ELEM3}{CDATA[
89]}{CDATA[    ]}{ENTREF[&included-entity;]}{CDATA[
90]}{CDATA[    ]}{ELEM4}{CDATA[
91]}{CDATA[     ]}{PI[test,processing instruction ]}{CDATA[
92]}{CDATA[    ]}{/ELEM4}{CDATA[
93]}{CDATA[   ]}{/ELEM3}{CDATA[
94]}{CDATA[  ]}{/ELEM2}{CDATA[
95]}{CDATA[ ]}{/ELEM1}{CDATA[
96]}{/ROOT}{?[
97]}parse complete
98