Below is the online
Copy Code code as follows:
Class Arraytoxml
{
/**
* The main function for converting to an XML document.
* Pass in a multi dimensional array and the recrusively loops through and builds up an XML document.
*
* @param array $data
* @param string $rootNodeName-what your want the root node to Be-defaultsto data.
* @param simplexmlelement $xml-should be used recursively
* @return String XML
*/
public static function ToXml ($data, $rootNodeName = ' data ', $xml =null)
{
Turn off compatibility mode as simple XML throws a wobbly if you don ' t.
if (Ini_get (' zend.ze1_compatibility_mode ') = = 1)
{
Ini_set (' Zend.ze1_compatibility_mode ', 0);
}
if ($xml = = null)
{
$xml = simplexml_load_string ("<?xml version= ' 1.0 ' encoding= ' utf-8 ' $rootNodeName?><");
}
Loop through the data passed in.
foreach ($data as $key => $value)
{
No numeric keys in our XML please!
if (Is_numeric ($key))
{
Make string key ...
$key = "Unknownnode_". (string) $key;
}
Replace anything not alpha numeric
$key = preg_replace ('/[^a-z]/i ', ', ', $key);
If there is another array found recrusively the call this function
if (Is_array ($value))
{
$node = $xml->addchild ($key);
Recrusive call.
Arraytoxml::toxml ($value, $rootNodeName, $node);
}
Else
{
Add single node.
$value = Htmlentities ($value);
$xml->addchild ($key, $value);
}
}
Pass is back as String. or simple XML object if you want!
return $xml->asxml ();
}
}
Here's the code I edited.
Copy Code code as follows:
function Arrtoxml ($arr, $dom =0, $item =0) {
if (! $dom) {
$dom = new DOMDocument ("1.0");
}
if (! $item) {
$item = $dom->createelement ("root");
$dom->appendchild ($item);
}
foreach ($arr as $key => $val) {
$itemx = $dom->createelement (is_string ($key) $key: "Item");
$item->appendchild ($ITEMX);
if (!is_array ($val)) {
$text = $dom->createtextnode ($val);
$itemx->appendchild ($text);
}else {
Arrtoxml ($val, $dom, $ITEMX);
}
}
return $dom->savexml ();
}
array into XML format
Copy Code code as follows:
?
$elementLevel = 0;
function Array_xml ($array, $keys = ')
{
Global $elementLevel;
if (!is_array ($array))
{
if ($keys = = ") {
return $array;
}else{
Return "\n< $keys >". $array. "</$keys >";
}
}else{
foreach ($array as $key => $value)
{
$haveTag = true;
if (Is_numeric ($key))
{
$key = $keys;
$haveTag = false;
}
/**
* the
*/
if ($elementLevel = = 0)
{
$startElement = "< $key >";
$endElement = "</$key >";
}
$text. = $startElement. " \ n ";
/**
* Other elements
*/
if (! $haveTag)
{
$elementLevel + +;
$text. = "< $key >". Array_xml ($value, $key). "</$key >\n";
}else{
$elementLevel + +;
$text. = Array_xml ($value, $key);
}
$text. = $endElement. " \ n ";
}
}
return $text;
}
?>
function Description and examples
Copy Code code as follows:
.
$array = Array (
"Employees" => Array (
"employee" => Array (
Array (
) name "=>" name one ",
Position" => "position one
),
Array (
" name "=>" Name Two ",
" position "=>" posi " tion two "
),
Array (
" name "=>" name three ",
" position "=>" position three "
)
)
)
);
Echo Array_xml ($array);
?>