|
|
xml_set_element_handler (PHP 3 >= 3.0.6, PHP 4, PHP 5) xml_set_element_handler -- Set up start and end element handlers Descriptionbool xml_set_element_handler ( resource parser, callback start_element_handler, callback end_element_handler )
Sets the element handler functions for the XML parser
parser.
start_element_handler and
end_element_handler are strings containing
the names of functions that must exist when
xml_parse() is called for
parser.
The function named by start_element_handler
must accept three parameters:
start_element_handler ( resource parser, string name, array attribs )
parser
The first parameter, parser, is a
reference to the XML parser calling the handler.
name
The second parameter, name, contains the name
of the element for which this handler is called.If case-folding is in effect for this
parser, the element name will be in uppercase letters.
attribs
The third parameter, attribs, contains an
associative array with the element's attributes (if any).The keys
of this array are the attribute names, the values are the attribute
values.Attribute names are case-folded on the same criteria as
element names.Attribute values are not
case-folded.
The original order of the attributes can be retrieved by walking
through attribs the normal way, using
each().The first key in the array was the first
attribute, and so on.
The function named by end_element_handler
must accept two parameters:
end_element_handler ( resource parser, string name )
parser
The first parameter, parser, is a
reference to the XML parser calling the handler.
name
The second parameter, name, contains the name
of the element for which this handler is called.If case-folding is in effect for this
parser, the element name will be in uppercase letters.
If a handler function is set to an empty string, or FALSE, the handler
in question is disabled.
TRUE is returned if the handlers are set up, FALSE if
parser is not a parser.
Замечание: В качестве аргумента вместо имени
функции может быть передан массив, содержащий ссылку на объект
и имя метода.
add a note
User Contributed Notes
xml_set_element_handler
darien at etelos dot com
12-Feb-2007 10:48
This documentation is somewhat awry. I know it's been said many times before, but it bears repeating...
If using PHP4, you may be required to use xml_set_object() instead of calling any of the xml_set_*_handler() functions with a two-item array. It will work fine on PHP5, but move the same code to PHP4 and it will create one copie of $this (even if you use &$this) for each handler you set!
<?php
$this->parser = xml_parser_create();
xml_set_element_handler(
$this->parser,
array(&$this,"start_tag"),
array(&$this,"end_tag")
);
xml_set_character_data_handler(
$this->parser,
array(&$this,"tag_data")
);
?>
<?php
$this->parser = xml_parser_create();
xml_set_object($this->parser,&$this);
xml_set_element_handler(
$this->parser,
"start_tag",
"end_tag"
);
xml_set_character_data_handler(
$this->parser,
"tag_data"
);
?>
lloeki at gmail dot com
31-Oct-2006 03:07
I modified the previous script, so that it is associative. I find it more useful that way. BTW I prefer strtolower() things, but that's not mandatory at all.
<?php
$file = "data.xml";
$depth = 0;
$tree = array();
$tree['name'] = "root";
$stack[] = &$tree;
function startElement($parser, $name, $attrs) {
global $depth;
global $stack;
global $tree;
$element = array();
foreach ($attrs as $key => $value) {
$element[strtolower($key)]=$value;
}
end($stack);
$stack[key($stack)][strtolower($name)] = &$element;
$stack[strtolower($name)] = &$element;
$depth++;
}
function endElement($parser, $name) {
global $depth;
global $stack;
array_pop($stack);
$depth--;
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "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",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
$tree = end(end($stack));
echo "<pre>";
print_r($tree);
echo "</pre>";
?>
rubentrancoso at gmail dot com
29-Aug-2006 01:44
My 25 cents. This example show how to parse a XML in a associative array tree.
<?php
$file = "flow/flow.xml";
$depth = 0;
$tree = array();
$tree['name'] = "root";
$stack[count($stack)] = &$tree;
function startElement($parser, $name, $attrs) {
global $depth;
global $stack;
global $tree;
$element = array();
$element['name'] = $name;
foreach ($attrs as $key => $value) {
$element[$key]=$value;
}
$last = &$stack[count($stack)-1];
$last[count($last)-1] = &$element;
$stack[count($stack)] = &$element;
$depth++;
}
function endElement($parser, $name) {
global $depth;
global $stack;
array_pop($stack);
$depth--;
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "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",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
$tree = $stack[0][0];
echo "<pre>";
print_r($tree);
echo "</pre>";
redb
15-May-2006 05:48
Example below (BadParser) works fine with some changes.
xml_set_element_handler ( $parser, array ( &$this, 'tagStart' ), array ( &$this, 'tagEnd' ) );
xml_set_character_data_handler ( $parser, array ( &$this, 'tagContent' ) );
Anonymous Koward
06-Apr-2006 10:38
This has been mentioned before, but I just spent several days trying to figure out what was going on. Folks, if your XML parser is completely in a class, look at the documentation for xml_set_object(). The documentation above does say you can use functions inside classes as callbacks for xml_set_element_handler, but it doesn't tell you that if your entire XML parser is inside a class, then this is the _WRONG_ way to do things.
You should instead call xml_set_object with your parser variable and $this, which will then fix strange errors that can otherwise crop up and stop you having to pass an array of ( $this, 'tagStart' ) to this function.
e.g.
<?php
class BadParser
{
function BadParser ()
{
$parser = xml_parser_create();
xml_set_element_handler ( $parser, array ( $this, 'tagStart' ), array ( $this, 'tagEnd' ) );
xml_set_character_data_handler ( $parser, array ( $this, 'tagContent' ) );
xml_parse ( $parser, $this->XMLData );
}
function tagStart ( $parser, $tagName, $attributes = NULL )
{
$this->tag = $tagName;
}
function tagEnd ( $parser, $tagName )
{
$this->tag = NULL;
}
function tagContent ( $parser, $content )
{
echo ( "{$this->tag}: $content" );
}
}
?>
Instead, you should change your constructor to do this as XML initalization instead:
<?php
class GoodParser
{
function GoodParser ()
{
$parser = xml_parser_create();
xml_set_object ( $parser, $this );
xml_set_element_handler ( $parser, 'tagStart', 'tagEnd' );
xml_set_character_data_handler ( $parser, 'tagContent' );
xml_parse ( $parser, $this->XMLData );
}
}
?>
I don't know if this problem exists in other versions of PHP. My version is 4.4.1. Hope I made sense, if this note had been around, it would've saved a lot of headaches for me (maybe I'm not observant enough).
vladimir-leontiev at uiowa dot edu
13-Dec-2005 09:44
It seems that characterData() gets characters in chuncks of 1024; therefore if you have string of characters between you tags that is longer than 1024 then characterData() will be called more that once for single pair of tags. I don't know if this feature(bug?) is documented anywhere, I just wanted to warn everyone about this; it had tripped me. I use php 4.3.10 on Linux.
hendra_g at hotmail dot com
19-Nov-2005 08:10
I ran into the same problem with 'ibjoel at hotmail dot com' in regards to self-closing tags, and found that the script that he/she wrote did not work as I expected.
I played around with some of php's functions and examples and compiled something, which may not be the neatest solution, but it works for the data that 'ibjoel at hotmail dot com' provided.
The data needs to be read from a file though, so the fp can be utilised. It still uses the xml_get_current_byte_index(resource parser) trick, but this time, I check for the last 2 character before the index and test if it's "/>".
<?php
$file = "myxmltest.xml";
$character_data_on = false;
$tag_complete = true;
function startElement($parser, $name, $attrs)
{
global $character_data_on;
global $tag_complete;
echo "<<font color=\"#0000cc\">$name</font>";
if (sizeof($attrs)) {
while (list($k, $v) = each($attrs)) {
echo " <font color=\"#009900\">$k</font>=\"<font
color=\"#990000\">$v</font>\"";
}
}
$tag_complete = false;
$character_data_on = false;
}
function endElement($parser, $name)
{
global $fp;
global $character_data_on;
global $tag_complete;
if (!$character_data_on) {
$temp_fp = ftell($fp);
$end_element_byte_index = xml_get_current_byte_index($parser);
fseek($fp,$end_element_byte_index-2);
$validator = fgets($fp, 3);
fseek($fp,$temp_fp);
if ($validator=="/>") {
echo " />";
} else echo "></<font color=\"#0000cc\">$name</font>>";
$tag_complete = true;
} else echo "</<font color=\"#0000cc\">$name</font>>";
$character_data_on = false;
}
function characterData($parser, $data)
{
global $character_data_on;
global $tag_complete;
if ((!$character_data_on)&&(!$tag_complete)) {
echo ">";
$tag_complete = true;
}
echo "<b>$data</b>";
$character_data_on = true;
}
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
echo "<pre>";
while ($file_content = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $file_content, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
echo "</pre>";
xml_parser_free($xml_parser);
?>
turan dot yuksel at tcmb dot gov dot tr
20-Sep-2005 03:41
The method that 'ibjoel at hotmail dot com' have described requires libxml2 as the xml parser, it does not work with expat. For a brief explanation, see xml_get_current_byte_index.
ibjoel at hotmail dot com
01-Aug-2005 03:49
I noticed that in the example below, and all the examples I've seen on this site for viewing xml in html, the look of self closing tags such as <br /> are not preserved. The parser cannot distinguish between <tag /> and <tag></tag>, and if your start and end element functions are like these examples, both instances will be output with both an indvidual start and end tag. I needed to preserve self-closing tags and it took me a while to figure out this work around. Hope this helps someone...
The start tag is left open, and then completed by it's first child, the next start tag or its end tag. The end tag will complete with " />", or </tag> depending on the number of bytes between the start and end tags in the parsed data.
<?php
$data=<<<DATA
<normal_tag>
<self_close_tag />
data
<normal_tag>data
<self_close_tag attr="value" />
</normal_tag>
data
<normal_tag></normal_tag>
</normal_tag>
DATA;
function startElement($parser, $name, $attrs)
{
xml_set_character_data_handler($parser, "characterData");
global $first_child, $start_byte;
if($first_child) echo "><br />";
$first_child=true;
$start_byte=xml_get_current_byte_index ($parser);
if(count($attrs)>=1){
foreach($attrs as $x=>$y){
$attr_string .= " $x=\"$y\"";
}
}
echo htmlentities("<{$name}{$attr_string}"); }
function endElement($parser, $name)
{
global $first_child, $start_byte;
$byte=xml_get_current_byte_index ($parser);
if($byte-$start_byte>2){ if($first_child) echo "><br />";
echo htmlentities("</{$name}>")."<br />"; }else
echo " /><br />"; $first_child=false;
}
function characterData($parser, $data)
{
global $first_child;
if($first_child) echo "><br />";
if($data=trim($data))
echo "<font color='blue'>$data</font><br />";
$first_child=false;
}
function ParseData($data)
{
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
if(is_file($data))
{
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
$error=xml_error_string(xml_get_error_code($xml_parser));
$line=xml_get_current_line_number($xml_parser);
die(sprintf("XML error: %s at line %d",$error,$line));
}
}
}else{
if (!xml_parse($xml_parser, $data, 1)) {
$error=xml_error_string(xml_get_error_code($xml_parser));
$line=xml_get_current_line_number($xml_parser);
die(sprintf("XML error: %s at line %d",$error,$line));
}
}
xml_parser_free($xml_parser);
}
ParseData($data);
?>
youniforever at naver dot com
30-Apr-2005 03:41
<html>
<head>
<title>SAX Demonstration</title>
<META HTTP-EQUIV='Content-type' CONTENT='text/html; charset=euc-kr'>
</head>
<body>
<h1>RSS
|
|