xref: /PHP-7.2/ext/xml/tests/xml001.phpt (revision 17ccbeec)
1--TEST--
2XML parser test, function 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
12$xml_parser = xml_parser_create();
13xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
14xml_set_element_handler($xml_parser, "startElement", "endElement");
15xml_set_character_data_handler($xml_parser, "characterData");
16xml_set_processing_instruction_handler($xml_parser, "PIHandler");
17xml_set_default_handler($xml_parser, "defaultHandler");
18xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");
19
20if (!($fp = @fopen("xmltest.xml", "r"))) {
21    die("could not open XML input");
22}
23
24while ($data = fread($fp, 4096)) {
25	if (!xml_parse($xml_parser, $data, feof($fp))) {
26		die(sprintf("XML error: %s at line %d\n",
27		xml_error_string(xml_get_error_code($xml_parser)),
28		xml_get_current_line_number($xml_parser)));
29	}
30}
31print "parse complete\n";
32xml_parser_free($xml_parser);
33
34function startElement($parser, $name, $attribs)
35{
36	print '{'.$name;
37	if (sizeof($attribs)) {
38        foreach ($attribs as $k => $v) {
39			print " $k=\"$v\"";
40		}
41	}
42	print '}';
43}
44
45function endElement($parser, $name)
46{
47	print '{/'.$name.'}';
48}
49
50function characterData($parser, $data)
51{
52	print '{CDATA['.$data.']}';
53}
54
55function PIHandler($parser, $target, $data)
56{
57	print '{PI['.$target.','.$data.']}';
58}
59
60function defaultHandler($parser, $data)
61{
62	if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
63		print '{ENTREF['.$data.']}';
64	} else {
65		print '{?['.$data.']}';
66	}
67}
68
69function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId, $publicId)
70{
71	print '{EXTENTREF['.$openEntityNames.','.$base.','.$systemId.','.$publicId."]}\n";
72	return true;
73}
74
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