<?php function Xml_encode ($data, $charset = ' utf-8 ', $root = ' so ') {$xml = ' <?xml version= ' 1.0 ' encod Ing= "'. $charset. ' "? > '; $xml. = "<{$root}>"; $xml. = Array_to_xml ($data); $xml. = "</{$root}>"; return $xml; } function Xml_decode ($xml, $root = ' so ') {$search = '/< ('. $root. ') > (. *) <\/\s*?\\1\s*?>/s '; $array = Array (); if (Preg_match ($search, $xml, $matches)) {$array = Xml_to_array ($matches [2]); } return $array; } function Array_to_xml ($array) {if (Is_object ($array)) {$array = Get_object_vars ($array); } $xml = '; foreach ($array as $key = = $value) {$_tag = $key; $_id = null; if (Is_numeric ($key)) {$_tag = ' item '; $_id = ' id= '. $key. ‘"‘; } $xml. = "<{$_tag}{$_id}>"; $xml. = (Is_array ($value) | | is_objeCT ($value))? Array_to_xml ($value): Htmlentities ($value); $xml. = "</{$_tag}>"; } return $xml; } function Xml_to_array ($xml) {$search = '/< (\w+) \s*? (?: [^\/>]*) \s* (?: \ />|> (. *?) <\/\s*?\\1\s*?>)/s '; $array = Array (); if (Preg_match_all ($search, $xml, $matches)) {foreach ($matches [1] as $i + = $key) {$value = $ matches[2][$i]; if (Preg_match_all ($search, $value, $_matches)) {$array [$key] = Xml_to_array ($value); }else{if (' ITEM ' = = Strtoupper ($key)) {$array [] = Html_entity_decode ($value); }else{$array [$key] = Html_entity_decode ($value); }}}} return $array; }
Array and XML transformation implementation (RPM)