Expat parser in XML

Expat parser in XML


Posted in : PHP Posted on : January 20, 2011 at 6:10 PM Comments : [ 0 ]

This section contains the detail about the use of XML expat parser in PHP.

Expat Parser in XML

XML is used for describing data. While XML file is used to describe the structure of the data. In XML, you need to define tags. There are no predefined tags.

An XML Parser is needed to create, read, update & manipulate an XML document.

XML parser is of two types :

Tree based parser : Using this type of parser, an XML document is transforms into a tree structure. After analyzing and transforming the XML document ,it also provides access to the tree elements. Ex. DOM(Document object model).

Event based parser : The Expat parser  is an event based parser. This type of parser views an XML document as a series of event. For example, consider the following XML fraction :

<sender>Tapas</sender>

The three events count by an event based parser is given below :

  • Start element: sender
  • Start CDATA section, value: Tapas
  • Close element: sender

When a specific event occurs, it calls a function to handle it.

For an XML document to be valid, the Document Type Definition (DTD) must be associated with it. But Expat is a an event-based, non-validating XML parser and ignores any DTDs.  Expat is fast and small, and a perfect match for PHP web applications.

Note : The Expat Parser functions are part of the PHP core. There is no need of extra installation for this parser. You can use it's functions directly.

Example :

Given below the XML file :

demo.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<phonebook>
<name>Ankit</name>
<surname>Kaushal</surname>
<mobile>99999999</mobile>
<location>New Delhi</location>
</phonebook>

ExpatParser.php

<?php
//Initialize the XML parser
$parser=xml_parser_create();

//Function to use at the start of an element
function start($parser,$element_name,$element_attrs)
{
switch($element_name)
{
case "PHONEBOOK":
echo "---Phonebook---<br />";
break;
case "NAME":
echo "Name: ";
break;
case "SURNAME":
echo "Surname: ";
break;
case "MOBILE":
echo "Mobile: ";
break;
case "LOCATION":
echo "Location: ";
}
}

//Function to use at the end of an element
function stop($parser,$element_name)
{
echo "<br />";
}

//Function to use when finding character data
function char($parser,$data)
{
echo $data;
}

//Specify element handler
xml_set_element_handler($parser,"start","stop");

//Specify data handler
xml_set_character_data_handler($parser,"char");

//Open XML file
$fp=fopen("XML/demo.xml","r");

//Read data
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)));
}
//Free the XML parser
xml_parser_free($parser);
?> 

Output :

When you run this PHP code you will get  following :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics