Classe ArrayToXML
{
/ **
* La funzione principale per la conversione di un documento XML.
* Passare in una matrice multidimensionale e questo loop recrusively attraverso e costruisce un documento XML.
*
* @ Param array di $ data
* @ Param stringa $ rootNodeName - ciò che si desidera che il nodo principale di essere - dati defaultsto.
* @ Param $ SimpleXMLElement xml - deve essere usato solo in modo ricorsivo
* @ Return stringa XML
* /
pubblico ToXml funzione statica ($ data, $ rootNodeName = 'data', $ xml = null)
{
/ / Disattivare la modalità di compatibilità, come semplice xml lancia una traballante se non lo fai.
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 attraverso i dati passati dentro
foreach ($ dati come $ key => $ value)
{
/ / Senza tasti numerici nel nostro xml per favore!
if (is_numeric ($ key))
{
/ / Crea stringa chiave ...
$ Key = "unknownNode_". (String) $ key;
}
/ / Sostituisce tutto ciò che non alfanumerico
$ Key = preg_replace ('/ [^ az] / i', ", $ key);
/ / Se c'è un altro array trovato recrusively chiamare questa funzione
if (is_array ($ value))
{
$ Nodo = $ xml-> addChild ($ key);
/ / Recrusive chiamata.
ArrayToXML :: ToXml ($ value, $ rootNodeName, $ node);
}
altro
{
/ / Aggiunge singolo nodo.
$ Valore = htmlentities ($ value);
$ Xml-> addChild ($ key, $ value);
}
}
/ / Passare di nuovo come stringa. o semplice oggetto xml se vuoi!
return $ xml-> asXML ();
}
}













































