честно говоря писался код давно и уверен что он работает.
class XmlConverter
{
/**
* Conver array in xml
*/
public function as_array(array $data, $xml = NULL)
{
if (is_null($xml))
{
$xml = simplexml_load_string('<'.key($data).'/>');
$data = current($data);
$return = TRUE;
}
if (is_array($data))
{
foreach ($data as $name => $value)
{
self::from_array($value, is_numeric($name) ? $xml : $xml->addChild($name));
}
}
else
{
$xml->{0} = $data;
}
if ( ! empty($return))
{
return $xml->asXML();
}
}
/**
* Conver xml in array
*/
public function to_array($xml)
{
$tree = NULL;
while($xml->read())
{
switch ($xml->nodeType)
{
case XMLReader::END_ELEMENT:
return $tree;
case XMLReader::ELEMENT:
$node = array(
'tag' => $xml->name,
'value' => $xml->isEmptyElement ? '' : self::to_array($xml)
);
if ($xml->hasAttributes)
{
while ($xml->moveToNextAttribute())
{
$node['attributes'][$xml->name] = $xml->value;
}
}
$tree[] = $node;
break;
case XMLReader::TEXT:
case XMLReader::CDATA:
$tree .= $xml->value;
}
}
return $tree;
}
}