Resources
Home
Downloads
Mailing list
Search this site
Link to this site

TT Web Site Manager
Documentation
Download
Tutorials
FAQ

TT calendar library
About
Documentation
Download
Demo

Other code
poptasticDB
Simple XML parser function
Email address validation function

TT articles
Mending a dead iBook battery
Battery guide for small devices
Build your own TV modulator
PHP Nipkow disk generator
The Napier coffeemaker

This function is a quick and dirty solution to parsing simple XML. It takes some XML code and a tag name and returns an array of the contents of all the ocurrences of that tag in the XML code. This does not offer a solution to all XML parsing problems, for that try PHP's XML parser functions, but it does provide a quick way to split data out of simple XML schemas.

Imagine a simple piece of XML as follows, passed to your PHP code as $xml:

<continent>
 <name>Europe</name>
 <country>UK</country>
 <country>France</country>
 <country>Spain</country>
</continent>
<continent>
 <name>South America</name>
 <country>Brazil</country>
 <country>Guyana</country>
 <country>Argentina</country>
</continent>

You can use the function at the bottom of this page to extract the details as follows:

#split up the continents and work through them one by one
$continent_array = get_tag_contents($xml,"continent");
print_r($continent_array);
foreach ($continent_array as $continent){
  #get the continent name
  $name_array = get_tag_contents($continent,"name");
  $name = $name_array[0];
  print_r($name);

  #get the country names
  $country_array = get_tag_contents($continent,"country");
  print_r($country_array);
}

And here is the function itself.

#function to return an array of the stuff between <tag> and </tag>
#for each occurence of <tag> for XML parsing.
#copyright (c)John W. List 2002-2003 http://www.technotoad.com
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

function get_tag_contents($xmlcode,$tag) {
  $i=0;
  $offset=0;
  $xmlcode = trim($xmlcode);
  do{ #Step through each ocurrence of <$tag>...</$tag> in the XML
    #find the next start tag
    $start_tag=strpos ($xmlcode,"<".$tag.">",$offset);
    $offset = $start_tag;
    #find the closing tag for the start tag you just found
    $end_tag=strpos ($xmlcode,"</".$tag.">",$offset);
    $offset = $end_tag;
    #split off <$tag>... as a string, leaving the </$tag> 
    $our_tag = substr ($xmlcode,$start_tag,($end_tag-$start_tag));
    $start_tag_length = strlen("<".$tag.">");
    if (substr($our_tag,0,$start_tag_length)=="<".$tag.">"){
      #strip off stray start tags from the beginning
      $our_tag = substr ($our_tag,$start_tag_length);
    }
    $array_of_tags[$i] =$our_tag;
    $i++;
  }while(!(strpos($xmlcode,"<".$tag.">",$offset)===false));
return $array_of_tags;
}

© copyright John W. List 1998 - 2007