xref: /PHP-8.2/ext/xml/tests/xml002.phpt (revision 8567bc10)
1--TEST--
2XML parser test, object tuple 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_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
55xml_set_element_handler($xml_parser, array($obj,"startElement"),
56array($obj, "endElement"));
57xml_set_character_data_handler($xml_parser, array($obj, "characterData"));
58xml_set_processing_instruction_handler($xml_parser, array($obj, "PIHandler"));
59xml_set_default_handler($xml_parser, array($obj, "defaultHandler"));
60xml_set_external_entity_ref_handler($xml_parser,
61array($obj, "externalEntityRefHandler"));
62
63if (!($fp = @fopen("xmltest.xml", "r"))) {
64    die("could not open XML input");
65}
66
67while ($data = fread($fp, 4096)) {
68    if (!xml_parse($xml_parser, $data, feof($fp))) {
69        die(sprintf("XML error: %s at line %d\n",
70        xml_error_string(xml_get_error_code($xml_parser)),
71        xml_get_current_line_number($xml_parser)));
72    }
73}
74print "parse complete\n";
75xml_parser_free($xml_parser);
76
77?>
78--EXPECT--
79{?[<?xml version="1.0" encoding="ISO-8859-1"?>]}{?[
80]}{?[<!DOCTYPE]}{?[ ]}{?[phptest]}{?[ ]}{?[SYSTEM]}{?[ ]}{?["notfound.dtd"]}{?[ ]}{?[[]}{?[
81]}{?[<!ENTITY]}{?[ ]}{?[%]}{?[ ]}{?[incent]}{?[ ]}{?[SYSTEM]}{?[ ]}{?["inc.ent"]}{?[>]}{?[
82]}{?[%incent;]}{?[
83]}{?[]]}{?[>]}{?[
84]}{ROOT ID="elem1"}{CDATA[
85]}{CDATA[ Plain text.]}{CDATA[
86]}{CDATA[ ]}{ELEM1}{CDATA[
87]}{CDATA[  ]}{?[<!-- comment -->]}{CDATA[
88]}{CDATA[  ]}{ELEM2}{CDATA[
89]}{CDATA[   ]}{?[<![CDATA[]}{CDATA[CDATA block]}{?[]]>]}{CDATA[
90]}{CDATA[   ]}{ELEM3}{CDATA[
91]}{CDATA[    ]}{ENTREF[&included-entity;]}{CDATA[
92]}{CDATA[    ]}{ELEM4}{CDATA[
93]}{CDATA[     ]}{PI[test,processing instruction ]}{CDATA[
94]}{CDATA[    ]}{/ELEM4}{CDATA[
95]}{CDATA[   ]}{/ELEM3}{CDATA[
96]}{CDATA[  ]}{/ELEM2}{CDATA[
97]}{CDATA[ ]}{/ELEM1}{CDATA[
98]}{/ROOT}{?[
99]}parse complete
100