Monday, 2 February 2015

PHP Recursive Array To XML With DOM

PHP Recursive Array To XML With DOM

This class provides a method to recursively create XML from a PHP array. The functionality is provided by extending the built in Document Object Model (DOM) class. By extending the DOM class, all the functions within are available locally to the array2xml class. The main engine in this class is the createNode method, which is a recursive function that adds the child nodes to the parent root directory.
The constructor takes two arguements, which are the name of the root node, and the default node_name which is applied to array elements that have a numeric index, as numeric XML tags are not permitted.

<?php
/**
 *
 * Array 2 XML class
 * Convert an array or multi-dimentional array to XML
 *
 * @author Kevin Waterson
 * @copyright 2009 PHPRO.ORG
 *
 */
class array2xml extends DomDocument{

    public 
$nodeName;

    private 
$xpath;

    private 
$root;

    private 
$node_name;


    
/**
    * Constructor, duh
    *
    * Set up the DOM environment
    *
    * @param    string    $root        The name of the root node
    * @param    string    $nod_name    The name numeric keys are called
    *
    */
    
public function __construct($root='root'$node_name='node')
    {
        
parent::__construct();

        
/*** set the encoding ***/
        
$this->encoding "ISO-8859-1";

        
/*** format the output ***/
        
$this->formatOutput true;

        
/*** set the node names ***/
        
$this->node_name $node_name;

        
/*** create the root element ***/
        
$this->root $this->appendChild($this->createElement$root ));

        
$this->xpath = new DomXPath($this);
    }

    
/*
    * creates the XML representation of the array
    *
    * @access    public
    * @param    array    $arr    The array to convert
    * @aparam    string    $node    The name given to child nodes when recursing
    *
    */
    
public function createNode$arr$node null)
    {
        if (
is_null($node))
        {
            
$node $this->root;
        }
        foreach(
$arr as $element => $value
        {
            
$element is_numeric$element ) ? $this->node_name $element;

            
$child $this->createElement($element, (is_array($value) ? null $value));
            
$node->appendChild($child);

            if (
is_array($value))
            {
                
self::createNode($value$child);
            }
        }
    }
    
/*
    * Return the generated XML as a string
    *
    * @access    public
    * @return    string
    *
    */
    
public function __toString()
    {
        return 
$this->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);
    }

// end of class
?>

EXAMPLE USAGE


$array = array(
        array(
            'name'=>'steve irwin',
            'address'=>
                array('number'=>1, 'street'=>'stingray'),
            'age'=>25),
        array(
                'name'=>'kylie minogue',
            'address'=>
                array('number'=>12, 'street'=>'bunny'),
            'age'=>48),
        array(
            'name'=>'rolf harris',
            'address'=>
                array('number'=>88, 'street'=>'didge'),
            'age'=>23),
        array(
            'name'=>'ian dury',
            'address'=>
                array('number'=>83, 'street'=>'drury'),
            'age'=>83)
    );

try 
{
    $xml = new array2xml('my_node');
    $xml->createNode( $array );
    echo $xml;

catch (Exception $e) 
{
    echo $e->getMessage();
}

?>

DEMONSTRATION

<?xml version="1.0" encoding="ISO-8859-1"?>
 <my_node>
  <node><name>steve irwin</name><address><number>1</number><street>stingray</street></address><age>25</age></node>
  <node><name>kylie minogue</name><address><number>12</number><street>bunny</street></address><age>48</age></node>
  <node><name>rolf harris</name><address><number>88</number><street>didge</street></address><age>23</age></node>
  <node><name>ian dury</name><address><number>83</number><street>drury</street></address><age>83</age></node>
</my_node>

0 comments:

Post a Comment