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 6 7 8 forwardlast
The Article

Creating the gallery

Now that we have verified that our parser works, we are ready to modify our parser to actually make use of our information. In order to do this, we only have to deal with our custom functions that handle the data.

Lets print out a nice little gallery using our images. Our gallery will just print the image with its dimentions, and a caption that is the name of the image. I will type out the modified functions, and then explain the code :

$current = "";
function start_tag($parser, $name, $attribs) {
   global $current;
   $current = $name;
   if ($name == "IMAGEINFO") { echo "<table border=\"1\" width=\"50%\">"; }
   if ($name == "IMAGE") { echo "<tr><td>"; }
   if ($name == "FILENAME") { echo "<img src=\""; }
   if ($name == "NAME") { echo "</div></td></tr><tr><td><div align=\"center\">"; }
   if ($name == "SIZE") {
   if (is_array($attribs)) {
      while(list($key,$val) = each($attribs)) {
echo strtolower($key)."=\"".$val."\"";
      }
   }
}
function end_tag($parser, $name) {
   if ($name == "NAME") { echo "</div></td></tr>"; }
   if ($name == "FILENAME") { echo " />"; }
   if ($name == "IMAGEINFO") { echo "</table>"; }
}
function tag_contents($parser, $data) {
   global $current;
   if ($current == "FILENAME") { echo $data; }
   if ($current == "NAME") { echo $data; }
}
 

Since the tag_contents() functions doesn’t get the name of the current tag from the parser, we have to manually provide it that information. In our start_tag() function, we set a global variable $current to the current tag name. The rest of the code is just checks to see which tag we are on, and print out the appropriate tags.

That's it! Now you have a "skeleton" parser that you can modify to use with XML files (such as RSS feeds).

You'll note that I am comparing tag names in upper case. The parser by default converts all tags to upper case. This behavior can be changed by passing arguments to the xml_parser_create() function.

View the highlighted source, download the source, or see it in action.

Pages: firstback1 2 3 4 5 6 7 8 forwardlast

Similar/related articles:


 
  Sponsors