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