Here's the online
Copy CodeThe code is as follows:
Class Arraytoxml
{
/**
* The main function for converting to an XML document.
* Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
*
* @param array $data
* @param string $rootNodeName-what want the root node to Be-defaultsto data.
* @param simplexmlelement $xml-should only 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 (" < $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 an array found recrusively call the 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 back as String. or simple XML object if you want!
return $xml->asxml ();
}
}
Here's the code I edited.
Copy CodeThe code is 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 CodeThe code is as follows:
$elementLevel = 0;
function Array_xml ($array, $keys = ")
{
Global $elementLevel;
if (!is_array ($array))
{
if ($keys = = ") {
return $array;
}else{
Return "\n< $keys >". $array. " ";
}
}else{
foreach ($array as $key = $value)
{
$haveTag = true;
if (Is_numeric ($key))
{
$key = $keys;
$haveTag = false;
}
/**
* The first element
*/
if ($elementLevel = = 0)
{
$startElement = "< $key >";
$endElement = " ";
}
$text. = $startElement. " \ n ";
/**
* Other elements
*/
if (! $haveTag)
{
$elementLevel + +;
$text. = "< $key >". Array_xml ($value, $key). " \ n ";
}else{
$elementLevel + +;
$text. = Array_xml ($value, $key);
}
$text. = $endElement. " \ n ";
}
}
return $text;
}
?>
function description and examples
Copy CodeThe code is as follows:
$array = Array (
"Employees" = = Array (
"Employee" = = Array (
Array
"Name" = "Name One",
"Position" = "position One"
),
Array
"Name" = "Name",
"Position" = "position"
),
Array
"Name" = "Name Three",
"Position" = "position three"
)
)
)
);
echo Array_xml ($array);
?>
http://www.bkjia.com/PHPjc/324027.html www.bkjia.com true http://www.bkjia.com/PHPjc/324027.html techarticle Below is the online copy code code as follows: Class Arraytoxml {/** * The main function for converting to an XML document. * Pass in a multi dime Nsional array and this recrusively ...