Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Parsing XML using PHP4

By Burhan Khalid
2005-08-16


Creating our XML File

Almost all the XML examples that I’ve seen use an address book example, but just to be different (and to make things interesting), we are going to create a XML file that contains information about the images in a folder and we will use this to create a very simple gallery.

The first step is to decide what information we want to store about the image. There are the usual suspects (the file name, the name of the image, the size) and we want to make sure this information is in our XML file. As I mentioned above, all XML documents contain a root tag. This will be the tag that will start and end our XML file. Lets call this tag <imageinfo>

The next step is to decide on what attributes a tag will have. An attribute is a property of the tag. For example, the <img> tag in HTML has the attribute src which tells user agent (browser) where to find the image.

For our example, we will create a size tag and give it the attributes width and height.

Most of the time developers are more concerned with parsing XML files rather than writing them, however, knowing the basics of what to expect in a XML file helps when trying to debug a parser.

Let write our very basic XML file :

XML:

  1. < ?xml version="1.0"?>
  2. <imageinfo>
  3.     <image>
  4. <filename>AmericanPie.jpg</filename>
  5. <size width="300" height="300" />
  6. <name>Mmm...Pie</name>
  7. </image>
  8. <image>
  9. <filename>Cantaloupe.jpg</filename>
  10. <size width="300" height="300" />
  11. <name>Cantaloupe</name>
  12. </image>
  13. <image>
  14. <filename>CitrusSlices.jpg</filename>
  15. <size width="300" height="300" />
  16. <name>Citrus For Summer</name>
  17. </image>
  18. </imageinfo>

< ?xml version="1.0"?> must be the first line in a XML file. It is called the xml declaration and identifies the file as a XML file to a parser.



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


 | Bookmark
Related Tutorials:
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources