Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Your Email:

Parsing XML using PHP4

By Burhan Khalid
2005-08-16


Setting up tag handlers

First, lets write out function that will be called for an open tag. The name of the function can be any valid PHP function name. The function must accept three arguments, and they must be $parser, $name, $attribs.

$parser = handle to our parser
$name = name of the current tag
$attrib = an array containing any attributes of the current tag

We don’t have to worry about calling the function, the parser does that automatically as it goes through our XML file. With that in mind, lets write our start tag function, which we will call start_tag (how creative, I know).

PHP:

  1. function start_tag($parser, $name, $attribs) {
  2.    echo "Current tag : ".$name."<br />";
  3.    if (is_array($attribs)) {
  4.       echo "Attributes : <br />";
  5.       while(list($key,$val) = each($attribs)) {
  6.          echo "Attribute ".$key." has value ".$val."<br />";
  7.        }
  8.     }
  9. }

Next, we will write our function that will be called when an ending tag is reached. This function, like our opening tag function, can be of any name that’s valid in PHP. The ending tag function must take these parameters $parser, $name.

$parser = handle to our parser
$name = name of the current tag

Lets write our ending tag function (which we will call end_tag):

PHP:

  1. function end_tag($parser, $name) {
  2.    echo "Reached ending tag ".$name."<br /><br />";
  3. }

We have now taken care of all the requirements for the xml_set_element_handler function, and now we can call it :

PHP:

  1.  
  2. xml_set_element_handler($xmlparser, "start_tag", "end_tag");
  3.  


Tutorial Pages:
» Parsing XML using PHP4
» XML File Structure
» Creating our XML File
» Parsing with PHP
» Creating our Parser
» Setting up tag handlers
» Setting up content (data) handlers
» Starting up the parser
» Creating the gallery


© 2004-2005 Burhan Khalid


 | Bookmark
Related Tutorials:
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK