<?php

/*
* array2xml class by Thomas Smit 24/06/2007 - creates an XML string representative of an array
* The array can then be outputted or queried using XPath, making accessing arrays very simple
*/

class array2xml
{

    private 
$dom;      // @var object $dom - PHP DOM object
    
private $xpath;    // @var object $xpath - DOM XPath object
    
private $arr;      // @var array $arr - hold the array to be converted
    
private $xmlstr;   // @var str $xmlstr - hold the generated XML

    /*
    * array2xml::__construct() - set array as class member, XML encoding and root XML element
    * @param array $arr - array to convert
    * @param str $enctype - XML encoding to use
    * @param str $root - what to name the root XML element
    * @return void
    */

    
public function __construct($arr$enctype$root)
    {
        
$this->arr $arr;
        
$this->dom = new DomDocument();
        
$this->xmlstr $this->createxml($enctype$root);
        
$this->dom->loadXML($this->xmlstr);
        
$this->xpath = new DomXPath($this->dom);
    }

    
/*
    * array2xml::genxml() - creates the XML representation of the array
    * @param array $arr - array to convert
    * @return void
    */

    
private function genxml($arr)
    {
        
$xmlstr =& $this->xmlstr;
        foreach(
$arr as $key => $val)
        {
            if(
is_array($val))
            {
                
$xmlstr .= (is_int($key))
                    ? 
'<undefined>' "\n"
                    
'<' $key '>' "\n";

                
$this->genxml($val);

                
$xmlstr .= (is_int($key))
                    ? 
'</undefined>' "\n"
                    
'</' $key '>' "\n";
            }
            else
            {
                
$xmlstr .= (is_int($key))
                    ? 
'<undefined>' $val '</undefined>' "\n"
                    
'<' $key '>' $val '</' $key '>' "\n";
            }
        }
    }

    
/*
    * array2xml::createxml() - create a proper XML string from the generated XML
    * @param str $enctype - XML encoding to use
    * @param str $root - what to name the root XML element
    * @return string
    */

    
private function createxml($enctype$root)
    {
        
$this->genxml($this->arr);
        return 
'<?xml version="1.0" encoding="' $enctype '"?>' "\n" '<' $root '>' "\n" $this->xmlstr '</' $root '>';
    }

    
/*
    * array2xml::getxml() - return the generated XML as a string
    * @return string
    */

    
public function getxml()
    {
        return 
$this->xmlstr;
    }

    
/*
    * array2xml::outputxml() - output the XML as XML
    * @return void
    */

    
public function outputxml()
    {
        
header('Content-type: application/xml');
        echo 
$this->dom->saveXML();
    }

    
/*
    * array2xml::query() - perform an XPath query on the XML representation of the array
    * @param str $query - query to perform
    * @return mixed
    */

    
public function query($query)
    {
        return 
$this->xpath->evaluate($query);
    }

}

?>