PHP Function Reference

PHP xml_parse() Function



The PHP xml_parse() function parses an XML document. The handlers for the configured events are called as many times as necessary.

Syntax

xml_parse(parser, data, is_final)

Parameters

parser Required. Specify a reference to the XML parser to use.
data Required. Specify the chunk of data to parse. A document can be parsed piece-wise by calling this function several times with new data, as long as the is_final parameter is set and true when the last data is parsed.
is_final Optional. If set to true, the data in the data parameter is the last piece of data sent in this parse.

Return Value

Returns 1 on success or 0 on failure.

For unsuccessful parses, error information can be retrieved with xml_get_error_code(), xml_error_string(), xml_get_current_line_number(), xml_get_current_column_number() and xml_get_current_byte_index().

Note: Some errors (such as entity errors) are reported at the end of the data, thus only if is_final is set and true.

Example:

Lets assume that we have a file called test.xml. This file contains following content:

<?xml version="1.0" encoding="UTF-8"?>
<userlist>
  <user>
    <username>John123</username>
    <name>John Smith</name>
    <phone>+1-8054098000</phone>
    <address>Brooklyn, New York, USA</address>
  </user>
</userlist>  

In the example below, an XML parser is created. Then the XML file is opened to parse its data using character handler function char_print. After parsing the document, the parser is freed using xml_parser_free() function.

<?php
//create an XML parser
$parser=xml_parser_create();

//character handler function for XML parser
function char_print($parser, $data) {
  echo $data;
}

//set the character handler function for XML parser
xml_set_character_data_handler($parser,"char_print");

//opening xml file
$fp = fopen("test.xml", "r");

while($data = fread($fp,4096)) {
  //parsing XML data
  xml_parse($parser,$data,feof($fp)) or

    //displaying error when parse error occurs
    die (sprintf("XML Error: %s at line %d",

    //error string
    xml_error_string(xml_get_error_code($parser)),

    //current line
    xml_get_current_line_number($parser)));
}

//free XML parser
xml_parser_free($parser);

fclose($fp);
?>

The output of the above code will be:

John123 John Smith +1-8054098000 Brooklyn, New York, USA

Example:

Consider one more example where the XML document is parsed using character handler function and element handler functions. After parsing the document, the parser is freed using xml_parser_free() function.

<?php
//create an XML parser
$parser=xml_parser_create();

//element handler function named "start_handler"
//enables customized output
function start_handler($parser, $element, $attribs) {     
  switch($element) {
    case "USERNAME":
      echo "<b>User Name : "; 
      break;
    case "NAME":
      echo "Name : ";         
      break;
    case "PHONE":
      echo "Phone : ";        
      break;
    case "ADDRESS":
      echo "Address : ";      
      break;
  }
}
 
//element handler function "end_handler"
function end_handler($parser, $element) {
  if($element == "USERNAME") 
    echo "</b><br>";
  else
    echo "<br>";
}
 
// Setting element handlers
xml_set_element_handler($parser, "start_handler", 
                        "end_handler");

//character handler function for XML parser
function char_print($parser, $data) {
  echo $data;
}

//set the character handler function for XML parser
xml_set_character_data_handler($parser,"char_print");

//opening xml file
$fp = fopen("test.xml", "r");

while($data = fread($fp,4096)) {
  //parsing XML data
  xml_parse($parser,$data,feof($fp)) or

    //displaying error when parse error occurs
    die (sprintf("XML Error: %s at line %d",

    //error string
    xml_error_string(xml_get_error_code($parser)),

    //current line
    xml_get_current_line_number($parser)));
}

//free XML parser
xml_parser_free($parser);

fclose($fp);
?>

The output of the above code will be:

User Name : John123
Name : John Smith
Phone : +1-8054098000
Address : Brooklyn, New York, USA

❮ PHP XML Parser Reference