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 8, 2008
Reader Question: What graphics compression program do you use?
About
 
August 7, 2008
Google's Big Mistake: Getting Rid of Google Page Creator, What Do...
About
 
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
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP

Parsing XML using PHP 

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

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. However, the most important thing to remember is the 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 like a property of the tag. For example, the <img> tag in HTML has the attribute src which tells browser where to find the image. For our example, we will give the size tag the attributes of 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 version="1.0"?>
<imageinfo>
    <image>
<filename>AmericanPie.jpg</filename>
<size width="300" height="300" />
<name>Mmm...Pie</name>
</image>
<image>
<filename>Cantaloupe.jpg</filename>
<size width="300" height="300" />
<name>Cantaloupe</name>
</image>
<image>
<filename>CitrusSlices.jpg</filename>
<size width="300" height="300" />
<name>Citrus For Summer</name>
</image>
</imageinfo>

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

Pages: firstback1 2 4 5 6 7 8 9 forwardlast

Similar/related articles:


 
  Sponsors