Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : XML Parser Functions : xml_parser_free

xml_parser_free

Free an XML parser (PHP 4, PHP 5)
bool xml_parser_free ( resource parser )

Frees the given XML parser.

Parameters

parser
A reference to the XML parser to free.

Return Values

This function returns FALSE if parser does not refer to a valid parser, or else it frees the parser and returns TRUE.

Examples ( Source code ) xml_parser_free

<?php
$file 
"contact.xml";
   
function 
startElement($parser$name$attrs) {
    print 
"<B>$name =></B>  ";
}

function 
endElement($parser$name) {
    print 
"n";
}
   
function 
characterData($parser$value) {
    print 
"$value<BR>";
}
   
$simpleparser xml_parser_create();
xml_set_element_handler($simpleparser"startElement""endElement");
xml_set_character_data_handler($simpleparser"characterData");
   
if (!(
$fp fopen($file"r"))) {
  die(
"could not open XML input");
}
   
while(
$data fread($fpfilesize($file))) {
  if (!
xml_parse($simpleparser$datafeof($fp))) {
     die(
xml_error_string(xml_get_error_code($simpleparser)));
  }
}

xml_parser_free($simpleparser);
?>
<!--
<contact id="43956">
     <personal>
          <name>
               <first>J</first>
               <middle>J</middle>
               <last>J</last>
          </name>
          <title>Manager</title>
          <employer>National Company</employer>
          <dob>1951-02-02</dob>
     </personal>
</contact>

-->

Related Examples ( Source code ) » xml_parser_free











Code Examples / Notes » xml_parser_free

alex_foe

whitout throwing exceptions you could try these for clear parser shutdown:
xml_parse($this->p, null, 1);
full example-class (php4 compatible):
class XML_Parser
{
   var $file = null;
   var $p = null; #the resource xml-parser
   var $__parsing = false;
   function XML_Parser($file = null)
   {
       if (!empty($file)) { $this->file = $file; }
       $this->p = xml_parser_create();
       xml_set_element_handler($this->p, array(&$this, "element_start"));
       
       register_shutdown_function(array(&$this, "destructor"));
   }
   function element_start($parser, $name, $attrs) { die("here I die while parsing"); }
   function parse_file($file = null)
   {
       $this->__parsing = true;
       if (!empty($file)) { $this->file = $file; }
       if (!file_exists($this->file)) { die("file does not exists: $this->file"); return; }
       if (!($fp = fopen($this->file, "r"))) { die("could not open XML input"); }
       while ($data = fread($fp, 4096))
       {
           if (!xml_parse($this->p, $data, feof($fp)))
           {
               die(
                     xml_error_string(xml_get_error_code($this->p)
                   . xml_getcurrent_line_number($this->p)));
           }
       }
       $this->__parsing = false;
   }
   function destructor()
   {
       if ($this->__parsing) { xml_parse($this->p, null, 1); }
       xml_parser_free($this->p);
   }
}
#run_me
$o =& new XML_Parser "some_xml_file.xml";
$o->parse_file();


dio

If you try to free an XML parser while it is still parsing, strange things will happen - your script will "hang" and no output will be sent to the browser. Consider this pseudo-code example:
-------
...
if (!xml_parse($parser)) echo 'XML error';
xml_parser_free($parser);
...
function SomeCallbackWhichWasSetBefore(...)
{
global $parser;
...
if (some_error_happened) xml_parser_free($parser);  //problem!
...
}
------
It would be logical that xml_parse would return false if the parser was freed while parsing, right? Wrong! Instead, everything hangs and no output will be sent out (no matter whether output buffering is on or not). It took me more than an hour to figure out why: you cannot free a parser handle that is currently parsing. A simple solution:
-------
$xml_error = false;
if (!xml_parse($parser))
echo 'XML error (directly from parser)';
else if ($xml_error)
echo 'XML error (from some callback function);
xml_parser_free($parser);
...
function SomeCallbackWhichWasSetBefore(...)
{
global $parser;
global $xml_error;
if ($xml_error)
return;
...
if (some_error_occured)
{
$xml_error = false;
return;
}
...
}
-------
If you use this solution you will have to check for $xml_error in every callback function. Essentially what you're doing is that, in case you want to stop parsing because of an error, you continue until xml_parse() is finished (without actually doing anything) and then free the parser and act on the error.
Of course the underlying problem is that you cannot stop a parser while it is parsing. There should be some function like xml_throw_error() or xml_parser_stop() or whatever, but unfortunately there isn't.


php dot net

I found that with PHP 4.3.4RC1, if you don't call xml_parser_free() before your script ends, some sort of ugliness occurs with the webserver; i.e. the HTTP connection is closed.  (The apache error_log says "exit signal Segmentation fault (11)".)
Usually PHP tends to clean up connections you don't explicitly close (e.g. database connections), but in this case apparently it doesn't.
Some web browsers (MSIE for one) do not actually show this problem, so you may not actually notice it.  Opera 7.11 does show it, which is how I discovered it.
So don't forget to call xml_parser_free() ... always!


Change Language


Follow Navioo On Twitter
utf8_decode
utf8_encode
xml_error_string
xml_get_current_byte_index
xml_get_current_column_number
xml_get_current_line_number
xml_get_error_code
xml_parse_into_struct
xml_parse
xml_parser_create_ns
xml_parser_create
xml_parser_free
xml_parser_get_option
xml_parser_set_option
xml_set_character_data_handler
xml_set_default_handler
xml_set_element_handler
xml_set_end_namespace_decl_handler
xml_set_external_entity_ref_handler
xml_set_notation_decl_handler
xml_set_object
xml_set_processing_instruction_handler
xml_set_start_namespace_decl_handler
xml_set_unparsed_entity_decl_handler
eXTReMe Tracker