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