feha Posted October 18, 2008 Share Posted October 18, 2008 Hi I have seen many classes but could not find a php class that could convert this array to XML: Array ( [1] => Array ( [id] => 1 [name] => Articles [class] => articles [link] => # [active] => 1 [group_id] => 0 [parent] => 0 [children] => Array ( [5] => Array ( [id] => 5 [name] => Add new [class] => add_article [link] => # [active] => 1 [group_id] => 0 [parent] => 1 [children] => Array ( ) ) [6] => Array ( [id] => 6 [name] => Categories [class] => categories [link] => # [active] => 1 [group_id] => 0 [parent] => 1 [children] => Array ( ) ) [8] => Array ( [id] => 8 [name] => Delete [class] => delete [link] => # [active] => 1 [group_id] => 0 [parent] => 1 [children] => Array ( ) ) ) ) [2] => Array ( [id] => 2 [name] => Users [class] => users [link] => # [active] => 1 [group_id] => 0 [parent] => 0 [children] => Array ( [7] => Array ( [id] => 7 [name] => Add new [class] => add_user [link] => # [active] => 1 [group_id] => 0 [parent] => 2 [children] => Array ( ) ) [9] => Array ( [id] => 9 [name] => Show [class] => show [link] => # [active] => 1 [group_id] => 0 [parent] => 2 [children] => Array ( [10] => Array ( [id] => 10 [name] => Last created [class] => last [link] => # [active] => 1 [group_id] => 0 [parent] => 9 [children] => Array ( ) ) [11] => Array ( [id] => 11 [name] => First created [class] => first [link] => # [active] => 1 [group_id] => 0 [parent] => 9 [children] => Array ( ) ) [12] => Array ( [id] => 12 [name] => All [class] => all [link] => # [active] => 1 [group_id] => 0 [parent] => 9 [children] => Array ( ) ) [13] => Array ( [id] => 13 [name] => None [class] => none [link] => # [active] => 1 [group_id] => 0 [parent] => 9 [children] => Array ( ) ) ) ) ) ) [3] => Array ( [id] => 3 [name] => Groups [class] => groups [link] => # [active] => 1 [group_id] => 0 [parent] => 0 [children] => Array ( ) ) [4] => Array ( [id] => 4 [name] => Settings [class] => settings [link] => # [active] => 1 [group_id] => 0 [parent] => 0 [children] => Array ( ) ) ) A tree structure, i need a way to make an xml structure from this but also get back from xml into array with same tree structure ... Any help ? Thank you in advance. Link to comment https://forums.phpfreaks.com/topic/128952-php-array-2-xml-and-xml-2-array/ Share on other sites More sharing options...
Acs Posted October 18, 2008 Share Posted October 18, 2008 This is from my xml helper class. Try it and let me know if it works public static function arraytoxml(array $data,$file,$elementname = "items") { $xml = simplexml_load_string(self::xmlcreate($file)); //we will always create the file if (!isset($xml->$elementname)) $xml->addChild($elementname); //Now let's add the data self::_arraytoxml($xml->$elementname,$data); //print_r($xml); return self::xmltofile($xml,$file); } // /** * This is what really creates the array * I had to seperate it so that I could use recursion * The values of the array, might be values them selfves * * @param SimpleXMLElement $xml * @param mixed $data */ private static function _arraytoxml(SimpleXMLElement &$xml,$data) { foreach ($data as $k => $value) { if (is_numeric($k)) $k = "_" . $k . "_"; //xml does not allow elements to just be numeric if (!isset($xml->$k)) $xml->addChild($k); if (is_array($value)) self::_arraytoxml($xml->$k,$value); else $xml->$k = self::xmlCdata($value); } } Link to comment https://forums.phpfreaks.com/topic/128952-php-array-2-xml-and-xml-2-array/#findComment-668541 Share on other sites More sharing options...
Acs Posted October 18, 2008 Share Posted October 18, 2008 Stupid edit timer!! The xmlcreate is just a simple method to create a dom object and then create an xml file public static function xmlcreate($file,$root = "root") { $dom = new DOMDocument('1.0', 'utf-8'); $element = $dom->appendChild(new DOMElement($root)); $xml = $dom->saveXML(); file_put_contents($file,$xml); return $xml; } Link to comment https://forums.phpfreaks.com/topic/128952-php-array-2-xml-and-xml-2-array/#findComment-668548 Share on other sites More sharing options...
feha Posted October 18, 2008 Author Share Posted October 18, 2008 Sorry i get this error: Parse error: syntax error, unexpected T_PUBLIC in C:\Inetpub\wwwroot\test\array22xml.php on line 2 I do have php5 but i must check if xml support is enabled ... Link to comment https://forums.phpfreaks.com/topic/128952-php-array-2-xml-and-xml-2-array/#findComment-668826 Share on other sites More sharing options...
Acs Posted October 18, 2008 Share Posted October 18, 2008 Did I really have to mention that in the code? Link to comment https://forums.phpfreaks.com/topic/128952-php-array-2-xml-and-xml-2-array/#findComment-668860 Share on other sites More sharing options...
feha Posted October 19, 2008 Author Share Posted October 19, 2008 No. However I want some solution that does not depend on php5. Thanks anyway ... Link to comment https://forums.phpfreaks.com/topic/128952-php-array-2-xml-and-xml-2-array/#findComment-669061 Share on other sites More sharing options...
Acs Posted October 19, 2008 Share Posted October 19, 2008 Well... I only do php5 no more php4 Link to comment https://forums.phpfreaks.com/topic/128952-php-array-2-xml-and-xml-2-array/#findComment-669083 Share on other sites More sharing options...
feha Posted October 19, 2008 Author Share Posted October 19, 2008 Thanks, my local server has php5 support but my hosting company has not yet moved to php5 :-(. I will make some custom functions (a temporary solution), but i like the great xml built in support in php5. Link to comment https://forums.phpfreaks.com/topic/128952-php-array-2-xml-and-xml-2-array/#findComment-669118 Share on other sites More sharing options...
DarkWater Posted October 19, 2008 Share Posted October 19, 2008 Just remove 'public' from in front because it's a keyword that works only in classes. :-\ Link to comment https://forums.phpfreaks.com/topic/128952-php-array-2-xml-and-xml-2-array/#findComment-669126 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.