PHP code for converting arrays into XML format

Source: Internet
Author: User

Below is the online
Copy codeThe Code is as follows:
Class ArrayToXML
{
/**
* The main function for converting to an XML document.
* Pass in a multi dimen1_array and this recrusively loops through and builds up an XML document.
*
* @ Param array $ data
* @ Param string $ rootNodeName-what you want the root node to be-defaultsto data.
* @ Param SimpleXMLElement $ xml-shoshould 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. zemo-compatibility_mode ') = 1)
{
Ini_set ('zend. zemo-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 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 back as string. or simple xml object if you want!
Return $ xml-> asXML ();
}
}

The code I edited is as follows:
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 ();
}

Convert an array to 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. "</$ keys> ";
}
} 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 = "</$ 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 example
Copy codeThe Code is as follows:
<?
$ Array = array (
"Employees" => array (
"Employee" => array (
Array (
"Name" => "name one ",
"Position" => "position one"
),
Array (
"Name" => "name two ",
"Position" => "position 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.