PHP.mk документација

xml_set_element_handler

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.xml-set-element-handler.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.xml-set-element-handler.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
xml_set_element_handler

Референца за `function.xml-set-element-handler.php` со подобрена типографија и навигација.

function.xml-set-element-handler.php

xml_set_element_handler

(PHP 4, PHP 5, PHP 7, PHP 8)

xml_set_element_handlerSet up start and end element handlers

= NULL

xml_set_element_handler(XMLParser $parser, callable|string|null $start_handler, callable|string|null $end_handler): true

Поставете почетни и крајни рачки за елементи parser.

start_handler Ги поставува функциите за ракување со елементи за XML end_handler се повикува кога ќе се отвори нов XML елемент.

Параметри

parser

XML парсерот.

start_handler

Враќа null се поминува, рачката се ресетира на својата стандардна состојба.

Ги ескејпува специјалните знаци во стринг за употреба во SQL изјава

Празен стринг исто така ќе ја ресетира рачката, меѓутоа ова е застарено од PHP 8.4.0.

Враќа handler е callable, повикливиот се поставува како рачка.

Враќа handler е string, тоа може да биде името на метод на објект поставен со xml_set_object().

Ги ескејпува специјалните знаци во стринг за употреба во SQL изјава

Ова е застарено од PHP 8.4.0.

Ги ескејпува специјалните знаци во стринг за употреба во SQL изјава

Од PHP 8.4.0, повикливиот се проверува да биде валиден при поставување на рачката, а не кога се повикува. Ова значи дека xml_set_object() мора да се повика пред поставување на стринг за метод како повратен повик. Меѓутоа, бидејќи ова однесување е исто така застарено од PHP 8.4.0, користењето на соодветен callable за методот се препорачува наместо тоа.

Сигнатурата на рачката мора да биде:

start_element_handler(XMLParser $parser, string $name, array $attributes): void
parser
XML парсерот што ја повикува рачката.
name
се повикува кога ќе се затвори XML елемент. case-folding Го содржи името на елементот за кој се повикува оваа рачка. Ако
attributes
е во сила за овој парсер, името на елементот ќе биде со големи букви. case-folded Асоцијативен список со атрибутите на елементот. Списокот е празен ако елементот нема атрибути. Клучевите на овој список се имињата на атрибутите, вредностите се вредностите на атрибутите. Имињата на атрибутите се not case-folded. според истите критериуми како имињата на елементите. Вредностите на атрибутите се attributes Редоследот по кој
end_handler

Враќа null се поминува, рачката се ресетира на својата стандардна состојба.

Ги ескејпува специјалните знаци во стринг за употреба во SQL изјава

Празен стринг исто така ќе ја ресетира рачката, меѓутоа ова е застарено од PHP 8.4.0.

Враќа handler е callable, повикливиот се поставува како рачка.

Враќа handler е string, тоа може да биде името на метод на објект поставен со xml_set_object().

Ги ескејпува специјалните знаци во стринг за употреба во SQL изјава

Ова е застарено од PHP 8.4.0.

Ги ескејпува специјалните знаци во стринг за употреба во SQL изјава

Од PHP 8.4.0, повикливиот се проверува да биде валиден при поставување на рачката, а не кога се повикува. Ова значи дека xml_set_object() мора да се повика пред поставување на стринг за метод како повратен повик. Меѓутоа, бидејќи ова однесување е исто така застарено од PHP 8.4.0, користењето на соодветен callable за методот се препорачува наместо тоа.

Сигнатурата на рачката мора да биде:

end_element_handler(XMLParser $parser, string $name): void
parser
XML парсерот што ја повикува рачката.
name
се повикува кога ќе се затвори XML елемент. case-folding Го содржи името на елементот за кој се повикува оваа рачка. Ако

Вратени вредности

Секогаш враќа true.

Дневник на промени

Верзија = NULL
8.4.0 Поминување на не-callable string to handler е сега застарено, користете соодветен повик за методи, или null за ресетирање на ракувачот.
8.4.0 Валидноста на handler како callable се проверува сега при поставување на ракувачот наместо при негово повикување.
8.0.0 parser очекува XMLParser инстанца сега; претходно, валидна xml resource се очекуваше.

Белешки од корисници — Интерпретира стринг од XML во објект

се движи е идентичен со редоследот по кој атрибутите биле декларирани.
19 години пред
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) { 
        //echo $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>";
rubentrancoso at gmail dot com
21 години пред
In response to landb at mail dot net...

As the notes mention, you can pass an array that contains the reference to an object and a method name when you need... so you can call methods in your own class as handlers like this:

xml_set_element_handler($parser, array($this,"_startElement"), array($this,"_endElement"));

Hope it helps...
aw at avatartechnology dot com
20 години пред
<html> 
  <head> 
    <title>SAX Demonstration</title> 
   <META HTTP-EQUIV='Content-type' CONTENT='text/html; charset=euc-kr'> 
  </head> 
  <body> 
    <h1>RSS ??????</h1> 
    
      <?php 

     $file = "data.xml"; 
      
      $currentTag = ""; 
      $currentAttribs = ""; 

      function startElement($parser, $name, $attribs) 
      { 
          global $currentTag, $currentAttribs; 
          $currentTag = $name; 
    
          $currentAttribs = $attribs; 
          switch ($name) { 
          
          default: 
              echo("<b>&lt$name&gt</b><br>"); 
              break; 
          } 
      } 

      function endElement($parser, $name) 
      { 
          global $currentTag; 
          switch ($name) { 
          default: 
              echo("<br><b>&lt/$name&gt</b><br><br>"); 
              break; 
          } 
          $currentTag = ""; 
          $currentAttribs = ""; 
      } 

      function characterData($parser, $data) 
      { 
          global $currentTag; 
          switch ($currentTag) { 
          case "link": 
              echo("<a href=\"$data\">$data</a>\n"); 
              break; 
          case "title": 
              echo("title : $data"); 
              break; 
          default: 
              echo($data); 
              break; 
          } 
      } 

     $xmlParser = xml_parser_create(); 
    
      $caseFold = xml_parser_get_option($xmlParser, 
                                        XML_OPTION_CASE_FOLDING); 
    
      $targetEncoding = xml_parser_get_option($xmlParser, 
                                              XML_OPTION_TARGET_ENCODING); 

      if ($caseFold == 1) { 
          xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, false); 
      } 

      xml_set_element_handler($xmlParser, "startElement", "endElement"); 
      xml_set_character_data_handler($xmlParser, "characterData"); 

      if (!($fp = fopen($file, "r"))) { 
          die("Cannot open XML data file: $file"); 
      } 

     while ($data = fread($fp, 4096)) { 
          if (!xml_parse($xmlParser, $data, feof($fp))) { 
              die(sprintf("XML error: %s at line %d", 
                          xml_error_string(xml_get_error_code($xmlParser)), 
                          xml_get_current_line_number($xmlParser))); 
              xml_parser_free($xmlParser); 
          } 
      } 
      xml_parser_free($xmlParser); 
      ?> 
    </table> 
  </body> 
</html>
youniforever at naver dot com
19 години пред
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>";

?>
lloeki at gmail dot com
20 години пред
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
/* myxmltest.xml:
<normal_tag>
  <self_close_tag />
     data
  <normal_tag>data
     <self_close_tag attr="value" />
  </normal_tag>
     data
  <normal_tag></normal_tag>
</normal_tag>
*/

//## Global Variables ##//
$file = "myxmltest.xml";
$character_data_on = false;
$tag_complete = true;

function startElement($parser, $name, $attrs) 
{
    global $character_data_on;
    global $tag_complete;
    
    echo "&lt;<font color=\"#0000cc\">$name</font>";
    //## Print the attributes ##//
    if (sizeof($attrs)) {
        while (list($k, $v) = each($attrs)) {
            echo " <font color=\"#009900\">$k</font>=\"<font 
                   color=\"#990000\">$v</font>\"";
        }
    }
    //## Tag is still still incomplete,
    //## will be completed at either endElement or characterData ##//
    $tag_complete = false;
    $character_data_on = false;
}

function endElement($parser, $name) 
{
    global $fp;
    global $character_data_on;
    global $tag_complete;
    
    //#### Test for self-closing tag ####//
    //## xml_get_current_byte_index(resource parser) when run in this
    //## function, gives the index at (indicated by *):
    //##   for self closing tag: <br />*
    //##   for individual closing tag: <div>character data*</div>
    //## So to test for self-closing tag, we can just test for the last 2 
    //## characters from the index
    //###################################//
    
    if (!$character_data_on) {
        //## Record current fp position ##//
        $temp_fp = ftell($fp);
        
        //## Point fp to 2 bytes before the end element byte index ##//
        $end_element_byte_index = xml_get_current_byte_index($parser);
        fseek($fp,$end_element_byte_index-2);
        
        //## Gets the last 2 characters before the end element byte index ##//
        $validator = fgets($fp, 3);
        
        //## Restore fp position ##//
        fseek($fp,$temp_fp);
        
        //## If the last 2 character is "/>" ##//
        if ($validator=="/>") {
            //// Complete the self-closing tag ////
            echo " /&gt";
            //// Otherwise it is an individual closing tag ////
        } else echo "&gt&lt/<font color=\"#0000cc\">$name</font>&gt";
        $tag_complete = true;
    } else echo "&lt/<font color=\"#0000cc\">$name</font>&gt";
    
    $character_data_on = false;
}

function characterData($parser, $data) 
{
    global $character_data_on;
    global $tag_complete;
    
    if ((!$character_data_on)&&(!$tag_complete)) {
        echo "&gt";
        $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);
?>
hendra_g at hotmail dot com
figroc at gmail dot com
If you are using a class for xml parsing, and want to check the return value of xml_set_element_handler in case it  fails, you must do this outside of the class's constructor. Inside the constructor, PHP-4.0.5 will die.

Basically, put all your xml initialisation code in another function of the class, and keep it out of the constructor.
jg at jmkg dot net
19 години пред
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 code will fail mysteriously on PHP4.
$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 code will work on PHP4.
$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"
        );
?>
darien at etelos dot com
20 години пред
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.
turan dot yuksel at tcmb dot gov dot tr
20 години пред
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=filepath or string
$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)          //close start tag if neccessary
                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}"); //unclosed starttag
}

function endElement($parser, $name)
{
        global $first_child, $start_byte;
        $byte=xml_get_current_byte_index ($parser);
        if($byte-$start_byte>2){           //if end tag is more than 2 bytes from start tag
                if($first_child)          //close start tag if neccessary
                        echo "><br />";
                echo htmlentities("</{$name}>")."<br />";  //individual end tag
        }else
                echo " /><br />";  // self closing tag
        $first_child=false;

}

function characterData($parser, $data)
{
        global $first_child;
        if($first_child)  //if $data is first child, close start tag
                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);
?>
ibjoel at hotmail dot com
пред 23 години
It seems that the tag handlers don't block on one another (the end handler is called whether or not the begin handler has finished). this can put you in a tight spot if you don't realize it while planning your app.
кок на нацон дот нк
пред 17 години
Here is another example of detecting empty elements. I works with libxml2. Note that it handles buffer boundaries.

<?php

$depth = 0; //current depth, used for pretty printing
$empty = false; //whether the tag is empty
$offset = 0; //the index of the start of the current buffer within the stream

function tagStart($parser, $name, array $attribs) {
    global $depth, $empty, $data, $offset, $lastchar;
    $idx = xml_get_current_byte_index($parser);
    /* xml_get_current_byte_index returns index within the streams and not
       within the buffer.*/
    
    /* Check if the index is within the buffer. */
    if (isset($data[$idx - $offset])) {
        $c = $data[$idx - $offset];
    } else {
        /* If it isn't simple use the last character of the buffer. */
        $c = $lastchar;
    }
    $empty = $c == '/';
    echo str_repeat("\t", $depth), "<$name", ($empty ? '/>' : '>'), "\n";
    if (!$empty) ++$depth;
}

function tagEnd($parser, $name) {
    global $depth, $empty;
    if (!$empty) {
        --$depth;
        echo str_repeat("\t", $depth), "</$name>\n";
    } else {
        $empty = false;
    }
}

$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($parser, 'tagStart', 'tagEnd');

$data1 = '
<test>
    <empty att="3" />
    <nocontent></nocontent>
    <content>
        <empty/>
        <empty/>
    </content>
    <empty/';

$data2 = '>
    <empty att="5" />
</test>
';

$data = &$data1;
$length = strlen($data1);
$lastchar = $data[$length-1];
xml_parse($parser, $data1);
$offset .= $length;
$data = &$data2;
xml_parse($parser, $data2);
владимир-леонтиев на уиова дот еду
20 години пред
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.
redb
19 години пред
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' ) );
Анонимен
21 години пред
In response to aw at avatartechnology dot com...
In response to landb at mail dot net...

When your functions are in an object:
Careful ! Don't forget to add: & (reference) to your parameters.

xml_set_element_handler($parser, array(&$this,"_startElement"), array(&$this,"_endElement"));
--> xmlparse will work on your object (good).

instead of:
xml_set_element_handler($parser, array($this,"_startElement"), array($this,"_endElement"));
---> xmlparse will work on a COPY of your object (often bad)

Vin-s
(sorry for my english)
Анонимен
figroc at gmail dot com
You CAN use classes to parse XML. Just take a look at the following function:

xml_set_object
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.