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 3, 2008
Poll: Which Web editor do you use?
About
 
July 3, 2008
Book Review: Head First JavaScript
WebReference.com
 
July 3, 2008
10 Things You Can Do With a Wiki
About
 
July 2, 2008
Web Host Reviews - 10 New Reviews
About
 
July 2, 2008
14 Reasons You Should Join a Social Network
About
 
July 1, 2008
Mark Boulton's Freelance Design Secrets
SitePoint
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

Parsing XML using PHP 

  Views:    80326
  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 6 7 8 9 forwardlast
The Article

Creating our Parser

The first step is to create and setup our parser. The xml_parser_create() function will create the parser for us, and return us a handle to that parser. We will then have to setup the different handlers so that the parser knows what to do with each type of information (be it an opening tag, a closing tag, stuff between the tag, etc). Lets first check to make sure that we can create our parser, which is perhaps the least confusing line of code :

if (! ($xmlparser = xml_parser_create()) )
{ 
   die ("Cannot create parser");
}

This code simply checks to see if we can create a parser. It will quit with an appropriate message, since if we can’t create the parser, there is no use in going any further. If your script quits here with the error message, then your PHP installation isn’t setup with the expat library. Most Unix/Linux based servers have the expat library as part of their PHP install. Check the xml reference section of the PHP manual for instructions on installing the expat library. Alternately, you can also send a support request to your host/ISP’s help desk.

Once we have created our parser, it is time to configure it to handle our XML file. The xml_set_element_handler() function takes three arguments. The first one is a handle to our xml_parser (which is $xmlparser). The next argument is the name of a function that the parser will call when it finds an open tag, and the last argument is the name of a function that the parser will call when it reaches an ending tag. We are going to write the functions that will be called for each open and close tag. Sounds scary, but it really is very straightforward.

Pages: firstback1 2 3 4 6 7 8 9 forwardlast

Similar/related articles:


 
  Sponsors