xref: /PHP-5.3/ext/xml/tests/xml003.phpt (revision 35343ffa)
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--INI--
9magic_quotes_runtime=0
10--FILE--
11<?php
12chdir(dirname(__FILE__));
13
14class myclass
15{
16	function startElement($parser, $name, $attribs)
17	{
18		print '{'.$name;
19		if (sizeof($attribs)) {
20			while (list($k, $v) = each($attribs)) {
21				print " $k=\"$v\"";
22			}
23		}
24		print '}';
25	}
26	function endElement($parser, $name)
27	{
28		print '{/'.$name.'}';
29	}
30	function characterData($parser, $data)
31	{
32		print '{CDATA['.$data.']}';
33	}
34	function PIHandler($parser, $target, $data)
35	{
36		print '{PI['.$target.','.$data.']}';
37	}
38	function defaultHandler($parser, $data)
39	{
40		if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
41			print '{ENTREF['.$data.']}';
42		} else {
43			print '{?['.$data.']}';
44		}
45	}
46	function externalEntityRefHandler($parser, $openEntityNames, $base,	$systemId, $publicId)
47	{
48		print '{EXTENTREF['.$openEntityNames.','.$base.','.$systemId.','.$publicId."]}\n";
49		return true;
50	}
51}
52
53$xml_parser = xml_parser_create();
54$obj = new myclass;
55xml_set_object($xml_parser, $obj);
56xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
57xml_set_element_handler($xml_parser, "startElement", "endElement");
58xml_set_character_data_handler($xml_parser, "characterData");
59xml_set_processing_instruction_handler($xml_parser, "PIHandler");
60xml_set_default_handler($xml_parser, "defaultHandler");
61xml_set_external_entity_ref_handler($xml_parser, "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--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