PHP to convert arrays into XML format implementation code _php tips

Source: Internet
Author: User
Tags addchild zend
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);
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.