iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

August 7, 2008
Wish XML a happy birthday
About
 
August 7, 2008
Poll: How important is SEO to your overal website strategy?
About
 
August 7, 2008
How to Create a Search Feature with PHP and MySQL
WebReference.com
 
August 7, 2008
1 comment
.net
 
August 6, 2008
10 New SEO Reports
About
 
August 5, 2008
Array Creator
EarthWeb.com
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

Parsing XML using PHP 

  Views:    84655
  Votes:    32
by Burhan Khalid 12/03/03 Rating: 

Synopsis:

This tutorial will show you how to parse XML files using the built-in PHP parser. The PHP parser (based on the expat library written by James Clark) is included with PHP installs. You can find out if your particular php installation has xml enabled by running the phpinfo(); command.
Pages: firstback1 2 3 4 5 7 8 9 forwardlast
The Article

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).

function start_tag($parser, $name, $attribs) {
   echo "Current tag : ".$name."<br />";
   if (is_array($attribs)) {
      echo "Attributes : <br />";
      while(list($key,$val) = each($attribs)) {
         echo "Attribute ".$key." has value ".$val."<br />";
       }
    }
}

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):

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

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

xml_set_element_handler($xmlparser, "start_tag", "end_tag");
Pages: firstback1 2 3 4 5 7 8 9 forwardlast

Similar/related articles:


 
  Sponsors