array2xml
public php v1 · immutable<?php /** * Simple Class for converting an array into XML * */ class array2xml { /** * Dom Instantiation * * @var Object */ public $data; /** * The current Dom tree * * @var Object */ public $dom_tree; /** * Basic constructor * * @param Array $array * @return Void */ public function __construct(Array $array, $root='result'){ if(!count($array)) { throw new Exception('array is empty', 2); unset($this); } $this->data = new DOMDocument('1.0'); $this->dom_tree = $this->data->createElement($root); $this->data->appendChild($this->dom_tree); $this->recurse_node($array, $this->dom_tree); } /** * method that recurses through an array * * @param array $data * @param DOMDocument $obj */ private function recurse_node( Array $data, DOMElement $obj) { $i = 0; foreach($data as $key=>$value){ if(is_array($value)){ $sub_obj[$i] = $this->data->createElement($key); $obj->appendChild($sub_obj[$i]); $this->recurse_node($value, $sub_obj[$i]); } elseif(is_numeric($key)) { /** * Numbers aren't valid tag names, so this creates an empty tag */ { $key = $value; $value = NULL; } $sub_obj[$i] = $this->data->createElement($key, $value); $obj->appendChild($sub_obj[$i]); } elseif (is_numeric(substr($key, 0,1))) { /** * Tags can't start with a number, so a leading number is the * only way to get multiple tags with the same name next to * each other. */ $key=ltrim($key,'01234567890'); $sub_obj[$i] = $this->data->createElement($key, $value); $obj->appendChild($sub_obj[$i]); } elseif (substr($key, 0,1) == "@" ) { /** * keys starting with an '@' indicate attributes */ $key=substr($key,1); $obj->setAttribute($key, $value); } elseif (substr($key, 0,1) == "?" ) { /** * ? is for processing instructions. * <?xml-stylesheet type='text/xsl' href='xsl/test.xsl'/> * NOTE: In this example even though type and href are * attributes, they're just passed as strings for the value. */ $key=substr($key,1); $sub_obj[$i] = $this->data->createProcessingInstruction($key, $value); //$this->data->firstChild->appendChild($sub_obj[$i]); $root=$this->data->firstChild; $root->parentNode->insertBefore($sub_obj[$i], $root); } else { $sub_obj[$i] = $this->data->createElement($key, $value); $obj->appendChild($sub_obj[$i]); } $i++; } } public function saveXML(){ return $this->data->saveXML(); } } if($_SERVER['SCRIPT_FILENAME'] == __FILE__) { //example $test = array( '?xml-stylesheet' => "type='text/xsl' href='xsl/test.xsl'", //processing instruction 'items'=> array( '@count' => 4, // attribute '0item' => 'zero', //multiple 'item' tags '1item' => 'one', //multiple 'item' tags '2item' => 'two', //multiple 'item' tags '3item' => 'three', //multiple 'item' tags ), 'test' => array( 'one', 'two', 'three', 'four') //numeric keys ); /** * This is the XML that this will generate. * I've indented the xml to make it easier to read * * <?xml version="1.0"?> * <?xml-stylesheet type='text/xsl' href='xsl/test.xsl'?> * <result> * <items count="4"> * <item>zero</item> * <item>one</item> * <item>two</item> * <item>three</item> * </items> * <test> * <one></one> * <two></two> * <three></three> * <four></four> * </test> * </result> */ try { $o_test = new array2xml($test); } catch (Exception $e) { echo $e->getMessage(); die(); } header ("content-type: text/xml"); echo $o_test->saveXML(); }