|
Helping ordinary people create extraordinary websites! |
Parsing XML using PHP4By Burhan Khalid2005-08-16
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 : PHP:
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). Notes 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. 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 |
|