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