Algo así
Código PHP:
Ver original<?php
$str = <<<XML
<foo>
<bar something="value">bar</bar>
<baz>baz</baz>
<candy>candy</candy>
<fruits>fruits</fruits>
<vegetable>vegetable</vegetable>
</foo>
XML;
function getArray($node)
{
$array = false;
if ($node->hasAttributes()){
foreach ($node->attributes as $attr){
$array['@' . $attr->nodeName] = $attr->nodeValue;
}
}
if ($node->hasChildNodes()){
if ($node->childNodes->length == 1){
$array[$node->firstChild->nodeName] = $node->firstChild->nodeValue;
}
else{
foreach ($node->childNodes as $childNode){
if ($childNode->nodeType != XML_TEXT_NODE){
$array[$childNode->nodeName][] = getArray($childNode);
}
}
}
}
return $array;
}
$xml = new DOMDocument();
$xml->formatOutput = false;
$xml->preserveWhiteSpace = false;
$xml->loadXML($str);
print_r(getArray
($xml->firstChild));
La función getArray la tome de aquí
http://www.php.net/manual/en/class.d...ent.php#101014