| 1 |
<?php |
| 2 |
class Array2XML { |
| 3 |
|
| 4 |
private static $xml = null; |
| 5 |
private static $encoding = 'UTF-8'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Initialize the root XML node [optional] |
| 9 |
* @param $version |
| 10 |
* @param $encoding |
| 11 |
* @param $format_output |
| 12 |
*/ |
| 13 |
public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) { |
| 14 |
self::$xml = new DomDocument($version, $encoding); |
| 15 |
self::$xml->formatOutput = $format_output; |
| 16 |
self::$encoding = $encoding; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Convert an Array to XML |
| 21 |
* @param string $node_name - name of the root node to be converted |
| 22 |
* @param array $arr - aray to be converterd |
| 23 |
* @return DomDocument |
| 24 |
*/ |
| 25 |
public static function &createXML($node_name, $arr=array()) { |
| 26 |
$xml = self::getXMLRoot(); |
| 27 |
$xml->appendChild(self::convert($node_name, $arr)); |
| 28 |
|
| 29 |
self::$xml = null; // clear the xml node in the class for 2nd time use. |
| 30 |
return $xml; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Convert an Array to XML |
| 35 |
* @param string $node_name - name of the root node to be converted |
| 36 |
* @param array $arr - aray to be converterd |
| 37 |
* @return DOMNode |
| 38 |
*/ |
| 39 |
private static function &convert($node_name, $arr=array()) { |
| 40 |
|
| 41 |
//print_arr($node_name); |
| 42 |
$xml = self::getXMLRoot(); |
| 43 |
$node = $xml->createElement($node_name); |
| 44 |
|
| 45 |
if(is_array($arr)){ |
| 46 |
// get the attributes first.; |
| 47 |
if(isset($arr['@attributes'])) { |
| 48 |
foreach($arr['@attributes'] as $key => $value) { |
| 49 |
if(!self::isValidTagName($key)) { |
| 50 |
throw new Exception('[Array2XML] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name); |
| 51 |
} |
| 52 |
$node->setAttribute($key, self::bool2str($value)); |
| 53 |
} |
| 54 |
unset($arr['@attributes']); //remove the key from the array once done. |
| 55 |
} |
| 56 |
|
| 57 |
// check if it has a value stored in @value, if yes store the value and return |
| 58 |
// else check if its directly stored as string |
| 59 |
if(isset($arr['@value'])) { |
| 60 |
$node->appendChild($xml->createTextNode(self::bool2str($arr['@value']))); |
| 61 |
unset($arr['@value']); //remove the key from the array once done. |
| 62 |
//return from recursion, as a note with value cannot have child nodes. |
| 63 |
return $node; |
| 64 |
} else if(isset($arr['@cdata'])) { |
| 65 |
$node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata']))); |
| 66 |
unset($arr['@cdata']); //remove the key from the array once done. |
| 67 |
//return from recursion, as a note with cdata cannot have child nodes. |
| 68 |
return $node; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
//create subnodes using recursion |
| 73 |
if(is_array($arr)){ |
| 74 |
// recurse to get the node for that key |
| 75 |
foreach($arr as $key=>$value){ |
| 76 |
if(!self::isValidTagName($key)) { |
| 77 |
throw new Exception('[Array2XML] Illegal character in tag name. tag: '.$key.' in node: '.$node_name); |
| 78 |
} |
| 79 |
if(is_array($value) && is_numeric(key($value))) { |
| 80 |
// MORE THAN ONE NODE OF ITS KIND; |
| 81 |
// if the new array is numeric index, means it is array of nodes of the same kind |
| 82 |
// it should follow the parent key name |
| 83 |
foreach($value as $k=>$v){ |
| 84 |
$node->appendChild(self::convert($key, $v)); |
| 85 |
} |
| 86 |
} else { |
| 87 |
// ONLY ONE NODE OF ITS KIND |
| 88 |
$node->appendChild(self::convert($key, $value)); |
| 89 |
} |
| 90 |
unset($arr[$key]); //remove the key from the array once done. |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// after we are done with all the keys in the array (if it is one) |
| 95 |
// we check if it has any text value, if yes, append it. |
| 96 |
if(!is_array($arr)) { |
| 97 |
$node->appendChild($xml->createTextNode(self::bool2str($arr))); |
| 98 |
} |
| 99 |
|
| 100 |
return $node; |
| 101 |
} |
| 102 |
|
| 103 |
/* |
| 104 |
* Get the root XML node, if there isn't one, create it. |
| 105 |
*/ |
| 106 |
private static function getXMLRoot(){ |
| 107 |
if(empty(self::$xml)) { |
| 108 |
self::init(); |
| 109 |
} |
| 110 |
return self::$xml; |
| 111 |
} |
| 112 |
|
| 113 |
/* |
| 114 |
* Get string representation of boolean value |
| 115 |
*/ |
| 116 |
private static function bool2str($v){ |
| 117 |
//convert boolean to text value. |
| 118 |
$v = $v === true ? 'true' : $v; |
| 119 |
$v = $v === false ? 'false' : $v; |
| 120 |
return $v; |
| 121 |
} |
| 122 |
|
| 123 |
/* |
| 124 |
* Check if the tag name or attribute name contains illegal characters |
| 125 |
* Ref: http://www.w3.org/TR/xml/#sec-common-syn |
| 126 |
*/ |
| 127 |
private static function isValidTagName($tag){ |
| 128 |
$pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i'; |
| 129 |
return preg_match($pattern, $tag, $matches) && $matches[0] == $tag; |
| 130 |
} |
| 131 |
} |
| 132 |
?> |
| 133 |
|