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

July 20, 2008
Wiki: A Solution to the Web Design Problem
About
 
July 19, 2008
Top 10 Social Networking Sites
About
 
July 19, 2008
Plurk or Twitter: Which one is Better?
About
 
July 19, 2008
Create Menus with Lists and CSS
About
 
July 17, 2008
Poll: What do you do when you can't design?
About
 
July 17, 2008
22 Reasons to Plurk
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

Parsing XML using PHP 

  Views:    82254
  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 9 forwardlast
The Article

Starting up the parser

Now that the parser is setup and configured, we are ready to feed it our XML file and let it parse the information. This is the complicated part of the program, so extra attention is requested.

The first step is to open the xml file :

$filename = "sample.xml";
if (!($fp = fopen($filename, "r"))) { die("cannot open ".$filename); }

This simple code will check to see if our program can open the file or not. It will quit with an appropriate message if it cannot.

Once the file is open, we must read it and feed it to the XML parser. One thing we are going to do before we send the file to the XML parser is we are going to get rid of any whitespace using a regular expression and the eregi_replace function :

while ($data = fread($fp, 4096)){
   $data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
   if (!xml_parse($xmlparser, $data, feof($fp))) {
      $reason = xml_error_string(xml_get_error_code($xmlparser));
      $reason .= xml_get_current_line_number($xmlparser);
      die($reason);
   }
}
xml_parser_free($xmlparser);

Lets step through this code :

  1. The fread() function reads the data from the xml file (given by the $fp handle), and stores it in $data.
  2. We use the eregi_replace function to get rid of the whitespace in $data
  3. We then check to see if the data was parsed or not, if it isn’t, we use the built-in xml error reporting functions to print out an informative error message.
  4. At the end, we free the parser (destory it)

Its always best to see the entire code at once, so you may view a highlighted version of the entire code, choose to download it, or see it in action.

Once we have verified that our parser is working properly, we are ready to actually do something with the data.

Pages: firstback1 2 3 4 5 6 7 9 forwardlast

Similar/related articles:


 
  Sponsors