Parse XML in PHP – Introduction

PHP XML Parser Functions

PHP XML parser extension includes several functions to access XML elements. Some of those functions are,

  • xml_parser_create()xml_parser_create_ns() – To create create XML parser handler. xml_parser_create_ns() function creates parser handler with namespace support.
  • xml_parse()xml_parse_into_struct() – To parse XML documentxml_parse_into_struct() is used to covert XML nodes into array.
  • xml_set_element_handler() – To set start and end element handlers for XML parser. There are more setters to set variety of handlers.
  • xml_get_current_line_number()xml_get_current_column_number() – To get current column line number and column number respectively.
  • xml_parser_free() – To cancel parser handler reference if it is not required.

Handler Functions

In XML Parser extension, handler functions are defined to invoke on a particular event. For example, start and end element handlers of this parser are invoked on element start and end respectively.

For XML parser a start element handler must contain parameters like parser handler, element name, and its attribute array. End element handler must contain parser handler and element name.

After definition, we need to set these element handlers to XML parser by using xml_parser_element_handler().

Parsing XML with Start and End Element Handlers

In this example program, we define onStart() and onEnd() functions as element event handlers. While parsing XML using xml_parse() these handlers display XML node structure with line numbers using PHP print statement.

$xmlDocument = '<?xml version="1.0"?>
<name>Ben 10 Watch</name>
<type>Battery Toys</type>
<name>Angry Birds Gun</name>
<type>Mechanical Toys</type>
$line_number = 0;
function onStart($parser,$name,$attributes) {
global $line_number;
if($line_number != xml_get_current_line_number($parser)) {
$line_number = xml_get_current_line_number($parser) . ": ";
$output = "<br/>" . $line_number . " \t<" . $name . ">";
} else 
$output = "<" . $name . ">";
echo $output;	
function onEnd($parser,$name) {
global $line_number;
if($line_number != xml_get_current_line_number($parser)) {
$line_number = xml_get_current_line_number($parser) . ": ";
$output = "<br/>".$line_number . " \t</" . $name . ">";
} else 
$output = "</" . $name . ">";
echo $output;	
$parser = xml_parser_create();
xml_set_element_handler($parser, 'onStart', 'onEnd');
if (!xml_parse($parser, $xmlDocument, true)) {
echo "<br/>Parse Error";
} else {
echo "<br/>Parsing is done.";

The output of this program is,

2: <TOYS>
3: <TOY>
4: <NAME></NAME>
5: <TYPE></TYPE>
6: </TOY>
7: <TOY>
8: <NAME></NAME>
9: <TYPE></TYPE>
10: </TOY>
11: </TOYS>
Parsing is done.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons
Social Media Auto Publish Powered By :