Created
April 11, 2024 07:20
-
-
Save Kcko/42864ba987390f4a3c3097f458d40f94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment