class_xml.php

Author: Troy Wolf (troy@troywolf.com)
Modified Date: 2006-03-06
Download: class_xml.zip
View class source: class_xml.php source

class_xml.php accepts raw XML input and returns a standard object that is easily enumerated for use in your scripts.

class_xml can be combined with class_http to cache xml data from third-party sites rather than hit the remove site every time your script is requested. class_http even includes methods to access XML data that is protected using Basic Authentication.

class_http and class_xml can be combined to create powerful RSS consumption tools. View an example RSS function.

To use the class_xml in your scripts, you first need to include the class file. Modify the path to fit your needs.
require_once(dirname(__FILE__).'/class_xml.php');

Instantiate a new xml object. You can create one object and use it over and over again throughout your script, or you can create multiple objects as needed.
$x = new xml();

Suppose you have an XML file like so:
    <cars>
      <make>
        Ford
        <model>
          Mustang
          <car id="12333" year="1998" mileage="105230" />
          <car id="98272" year="2004" mileage="25100" />
          <car id="12233" year="1999" mileage="112000" />
        </model>
        <model>
          Escort
          <car id="55437" year="1996" mileage="108700" />
          <car id="68238" year="1992" mileage="150000" />
        </model>
      </make>
      <make>
        Toyota
        <model>
          Prius
          <car id="13252" year="2005" mileage="10500" />
        </model>
        <model>
          Camry
          <car id="13789" year="2002" mileage="38080" />
          <car id="11201" year="2000" mileage="95663" />
        </model>
      </make>
    </cars>
    
    

The class is very easy to use. Use fetch() to pass in any standard XML data text. This can be an XML file on your own server, a remote server, or a URL to a web page that returns XML data. Assuming the XML example above is located on your webserver at "/foo/bar/mydata.xml", you could use the code below to parse it.
#$source = file_get_contents("http://rss.news.yahoo.com/rss/topstories");
$source = file_get_contents("/foo/bar/mydata.xml");

if (!
$x->fetch($source)) {
  
/*
  The class has a 'log' property that contains a log of events. This log is
  useful for testing and debugging.
  */
  
echo "<h2>There was a problem parsing your XML!</h2>";
  echo
$x->log;
  exit();
}

Once you have executed the fetch() method, you'll have a 'data' property that contains the parsed XML data. You can display the contents of the data object to help you understand how to work with the data.
echo "<pre>\n";
print_r($x->data);
echo
"</pre>\n";

....which would output the structure below. This may look a bit scary, but it's not that bad! I'll show you how easy it is to work with this object.
stdClass Object
(
    [
CARS] => Array
        (
            [
0] => stdClass Object
                
(
                    [
MAKE] => Array
                        (
                            [
0] => stdClass Object
                                
(
                                    [
_text] => Ford
                                    
[MODEL] => Array
                                        (
                                            [
0] => stdClass Object
                                                
(
                                                    [
_text] => Mustang
                                                    
[CAR] => Array
                                                        (
                                                            [
0] => stdClass Object
                                                                
(
                                                                    [
_attr] => stdClass Object
                                                                        
(
                                                                            [
ID] => 12333
                                                                            
[YEAR] => 1998
                                                                            
[MILEAGE] => 105230
                                                                        
)
                                                                )
                                                            [
1] => stdClass Object
                                                                
(
                                                                    [
_attr] => stdClass Object
                                                                        
(
                                                                            [
ID] => 98272
                                                                            
[YEAR] => 2004
                                                                            
[MILEAGE] => 25100
                                                                        
)
                                                                )
                                                            [
2] => stdClass Object
                                                                
(
                                                                    [
_attr] => stdClass Object
                                                                        
(
                                                                            [
ID] => 12233
                                                                            
[YEAR] => 1999
                                                                            
[MILEAGE] => 112000
                                                                        
)
                                                                )
                                                        )
                                                )
                                            [
1] => stdClass Object
                                                
(
                                                    [
_text] => Escort
                                                    
[CAR] => Array
                                                        (
                                                            [
0] => stdClass Object
                                                                
(
                                                                    [
_attr] => stdClass Object
                                                                        
(
                                                                            [
ID] => 55437
                                                                            
[YEAR] => 1996
                                                                            
[MILEAGE] => 108700
                                                                        
)
                                                                )
                                                            [
1] => stdClass Object
                                                                
(
                                                                    [
_attr] => stdClass Object
                                                                        
(
                                                                            [
ID] => 68238
                                                                            
[YEAR] => 1992
                                                                            
[MILEAGE] => 150000
                                                                        
)
                                                                )
                                                        )
                                                )
                                        )
                                )
                            [
1] => stdClass Object
                                
(
                                    [
_text] => Toyota
                                    
[MODEL] => Array
                                        (
                                            [
0] => stdClass Object
                                                
(
                                                    [
_text] => Prius
                                                    
[CAR] => Array
                                                        (
                                                            [
0] => stdClass Object
                                                                
(
                                                                    [
_attr] => stdClass Object
                                                                        
(
                                                                            [
ID] => 13252
                                                                            
[YEAR] => 2005
                                                                            
[MILEAGE] => 10500
                                                                        
)
                                                                )
                                                        )
                                                )
                                            [
1] => stdClass Object
                                                
(
                                                    [
_text] => Camry
                                                    
[CAR] => Array
                                                        (
                                                            [
0] => stdClass Object
                                                                
(
                                                                    [
_attr] => stdClass Object
                                                                        
(
                                                                            [
ID] => 13789
                                                                            
[YEAR] => 2002
                                                                            
[MILEAGE] => 38080
                                                                        
)
                                                                )
                                                            [
1] => stdClass Object
                                                                
(
                                                                    [
_attr] => stdClass Object
                                                                        
(
                                                                            [
ID] => 11201
                                                                            
[YEAR] => 2000
                                                                            
[MILEAGE] => 95663
                                                                        
)
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )
)

You can now direct access any item or iterate the items.
foreach ($x->data->CARS[0]->MAKE as $make) {
  echo
"<br />".$make->_text;
}

The code above would output:
    Ford
    Toyota
    

If you wanted to know about every Camry available, you could do something like this:
foreach ($x->data->CARS[0]->MAKE[1]->MODEL[1]->CAR as $car) {
  echo
"<hr />ID: ".$car->_attr->ID;
  echo
"<br />YEAR: ".$car->_attr->YEAR;
  echo
"<br />MILEAGE: ".number_format($car->_attr->MILEAGE,0);
}

This would output:
    
ID: 13789 YEAR: 2002 MILEAGE: 38,080
ID: 11201 YEAR: 2000 MILEAGE: 95,663

Finally, anytime you have problems, be sure to look at the 'log' property which will give you specific information related to problems with parsing your XML.
echo "<h1>Log</h1>";
echo
$x->log;


About the author

Troy Wolf is the author of SnippetEdit--a PHP application providing browser-based website editing that even non-technical people can use. Website editing as easy as it gets. Troy has been a professional Internet and database application developer for over 12 years. He has many years' experience with ASP, VBScript, PHP, Javascript, DHTML, CSS, SQL, and XML on Windows and Linux platforms. Check out Troy's Code Library.