xref: /PHP-7.2/ext/xml/tests/xml002.phpt (revision 06a03401)
1--TEST--
2XML parser test, object tuple 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_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
54xml_set_element_handler($xml_parser, array($obj,"startElement"),
55array($obj, "endElement"));
56xml_set_character_data_handler($xml_parser, array($obj, "characterData"));
57xml_set_processing_instruction_handler($xml_parser, array($obj, "PIHandler"));
58xml_set_default_handler($xml_parser, array($obj, "defaultHandler"));
59xml_set_external_entity_ref_handler($xml_parser,
60array($obj, "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?>
77--EXPECT--
78{?[<?xml version="1.0" encoding="ISO-8859-1"?>]}{?[
79]}{?[<!DOCTYPE]}{?[ ]}{?[phptest]}{?[ ]}{?[SYSTEM]}{?[ ]}{?["notfound.dtd"]}{?[ ]}{?[[]}{?[
80]}{?[<!ENTITY]}{?[ ]}{?[%]}{?[ ]}{?[incent]}{?[ ]}{?[SYSTEM]}{?[ ]}{?["inc.ent"]}{?[>]}{?[
81]}{?[%incent;]}{?[
82]}{?[]]}{?[>]}{?[
83]}{ROOT ID="elem1"}{CDATA[
84]}{CDATA[ Plain text.]}{CDATA[
85]}{CDATA[ ]}{ELEM1}{CDATA[
86]}{CDATA[  ]}{?[<!-- comment -->]}{CDATA[
87]}{CDATA[  ]}{ELEM2}{CDATA[
88]}{CDATA[   ]}{?[<![CDATA[]}{CDATA[CDATA block]}{?[]]>]}{CDATA[
89]}{CDATA[   ]}{ELEM3}{CDATA[
90]}{CDATA[    ]}{ENTREF[&included-entity;]}{CDATA[
91]}{CDATA[    ]}{ELEM4}{CDATA[
92]}{CDATA[     ]}{PI[test,processing instruction ]}{CDATA[
93]}{CDATA[    ]}{/ELEM4}{CDATA[
94]}{CDATA[   ]}{/ELEM3}{CDATA[
95]}{CDATA[  ]}{/ELEM2}{CDATA[
96]}{CDATA[ ]}{/ELEM1}{CDATA[
97]}{/ROOT}{?[
98]}parse complete
99