PHP - Function XML Parse



Syntax

int xml_parse ( resource $parser , string $data [, bool $is_final = false ] )

Definition and Usage

It used to parse parse an XML File

Return Values

It returns 1 on success or 0 on failure

Parameters

Parameters Discription
parser It is used to specifies XML parser to use.
xml It is used to specifies XML data to parse.
end If this parameter is TRUE, the data in the "xml" parameter is the last piece of data sent in this parse.

Example

Try out the following example

XML file as shown below

<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<?php
     $parser=xml_parser_create();

     function char($parser,$data)
     {
     echo $data;
     }
     xml_set_character_data_handler($parser,"char");
     $fp=fopen("test.xml","r");
     while ($data=fread($fp,4096))
     {
     xml_parse($parser,$data,feof($fp)) or 
     die (sprintf("XML Error: %s at line %d", 
     xml_error_string(xml_get_error_code($parser)),
     xml_get_current_line_number($parser)));
     }
     xml_parser_free($parser);
?>

The result should be as follows

Tove Jani Reminder Don't forget me this weekend!
php_function_reference.htm
Advertisements