File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / xml / tests / xml001.phpt
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:34:45 2012 UTC (12 years, 5 months ago) by misho
Branches: php, MAIN
CVS tags: v5_4_3elwix, v5_4_29p0, v5_4_29, v5_4_20p0, v5_4_20, v5_4_17p0, v5_4_17, HEAD
php 5.4.3+patches

--TEST--
XML parser test, function callbacks
--SKIPIF--
<?php
require_once("skipif.inc");
XML_SAX_IMPL == 'libxml' && die('skip this test is not intended for libxml SAX parser');
?>
--FILE--
<?php
chdir(dirname(__FILE__));

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_set_processing_instruction_handler($xml_parser, "PIHandler");
xml_set_default_handler($xml_parser, "defaultHandler");
xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");
    
if (!($fp = @fopen("xmltest.xml", "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
	if (!xml_parse($xml_parser, $data, feof($fp))) {
		die(sprintf("XML error: %s at line %d\n",
		xml_error_string(xml_get_error_code($xml_parser)),
		xml_get_current_line_number($xml_parser)));
	}
}
print "parse complete\n";
xml_parser_free($xml_parser);

function startElement($parser, $name, $attribs)
{
	print '{'.$name;
	if (sizeof($attribs)) {
		while (list($k, $v) = each($attribs)) {
			print " $k=\"$v\"";
		}
	}
	print '}';
}

function endElement($parser, $name)
{
	print '{/'.$name.'}';
}

function characterData($parser, $data)
{
	print '{CDATA['.$data.']}';
}

function PIHandler($parser, $target, $data)
{
	print '{PI['.$target.','.$data.']}';
}

function defaultHandler($parser, $data)
{
	if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
		print '{ENTREF['.$data.']}';
	} else {
		print '{?['.$data.']}';
	}
}

function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId, $publicId)
{
	print '{EXTENTREF['.$openEntityNames.','.$base.','.$systemId.','.$publicId."]}\n";
	return true;
}

?>
--EXPECT--
{?[<?xml version="1.0" encoding="ISO-8859-1"?>]}{?[
]}{?[<!DOCTYPE]}{?[ ]}{?[phptest]}{?[ ]}{?[SYSTEM]}{?[ ]}{?["notfound.dtd"]}{?[ ]}{?[[]}{?[
]}{?[<!ENTITY]}{?[ ]}{?[%]}{?[ ]}{?[incent]}{?[ ]}{?[SYSTEM]}{?[ ]}{?["inc.ent"]}{?[>]}{?[
]}{?[%incent;]}{?[
]}{?[]]}{?[>]}{?[
]}{ROOT ID="elem1"}{CDATA[
]}{CDATA[ Plain text.]}{CDATA[
]}{CDATA[ ]}{ELEM1}{CDATA[
]}{CDATA[  ]}{?[<!-- comment -->]}{CDATA[
]}{CDATA[  ]}{ELEM2}{CDATA[
]}{CDATA[   ]}{?[<![CDATA[]}{CDATA[CDATA block]}{?[]]>]}{CDATA[
]}{CDATA[   ]}{ELEM3}{CDATA[
]}{CDATA[    ]}{ENTREF[&included-entity;]}{CDATA[
]}{CDATA[    ]}{ELEM4}{CDATA[
]}{CDATA[     ]}{PI[test,processing instruction ]}{CDATA[
]}{CDATA[    ]}{/ELEM4}{CDATA[
]}{CDATA[   ]}{/ELEM3}{CDATA[
]}{CDATA[  ]}{/ELEM2}{CDATA[
]}{CDATA[ ]}{/ELEM1}{CDATA[
]}{/ROOT}{?[
]}parse complete

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>