tags:

views:

42

answers:

1

I'm working with various XML inputs and outputs and simply want to grab the keys and values like you can with an array.

Is there a simple function that can convert xml to an array or access the keys and values or am I going about this completely the wrong way?

<?php
$xmlstr = "
<key>
    <parent1>
     <child1>val1</child1>
     <child2>val2</child2>
    </parent1>
    <parent2>val4</parent2>
    <parent3>
     <child1 var="1">
      <gchild>
       <child>value</child>
       <child>value</child>
       <parent>
        <child>value</child>
       </parent>
      </gchild>
     </child1>
    </parent3>
</key>";

$xml = new SimpleXMLElement($xmlstr);
$xmlarray= convertXMLtoArray($xml);

echo $xmlarray[0]; //outputs: key
echo $xmlarray['key'][1]; //outputs: parent2 or array(child1->val1, child2->...)
echo $xmlarray['key']['parent1']['child1'][0]; //outputs: val1
?>
+3  A: 

simplexml_load_file

$xml = simplexml_load_file('test.xml');
var_dump($xml);
adatapost
+1, that's just awesome! :)
Mr. Smith
works well... but doesn't like <child1 var="val">
Peter
http://us3.php.net/manual/en/function.simplexml-load-file.php#84704
Mr. Smith
Attributes of an element are accessed like array elements. $child1['var']
VolkerK