I thought I should share this with you.
There are many times when you wish to convert your XML to a PHP Array.
Two things you should know
- There is no inbuilt PHP function to address this problem
- There are many ways to address this problem
Well lets get started.
Say you have an XML such as one below
$xml= '<persons> <person> <name>Jaspreet Chahal</name> <age>33</age> <city>Melbourne</city> </person> <person> <name>Yuvraaj Chahal</name> <age>2</age> <city>Adelaide</city> </person> </persons>';
Now do this
Now just print that array as
You will get result similar to this
stdClass Object ( ( [0] => stdClass Object ( [name] => Jaspreet Chahal [age] => 33 [city] => Melbourne ) [1] => stdClass Object ( [name] => Yuvraaj Chahal [age] => 2 [city] => Adelaide ) ) )
Said that this is not a full or optimal solution because this method does not handle Element Attributes well. More interesting and good approach will be the one shown below. Please note: The Code below is taken from PHP.net. I did not write this code. I’ve changed it a bit to accept XML as string rather than reading from file.
function xml2array($contents, $get_attributes = 1, $priority = 'tag') { { } if (!$xml_values) return; $current = & $xml_array; foreach ($xml_values as $data) { { if ($priority == 'tag') $result = $value; else $result['value'] = $value; } { foreach ($attributes as $attr => $val) { if ($priority == 'tag') $attributes_data[$attr] = $val; else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr' } } if ($type == "open") { $parent[$level -1] = & $current; { $current[$tag] = $result; if ($attributes_data) $current[$tag . '_attr'] = $attributes_data; $repeated_tag_index[$tag . '_' . $level] = 1; $current = & $current[$tag]; } else { { $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; $repeated_tag_index[$tag . '_' . $level]++; } else { $current[$tag], $result ); $repeated_tag_index[$tag . '_' . $level] = 2; { $current[$tag]['0_attr'] = $current[$tag . '_attr']; } } $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1; $current = & $current[$tag][$last_item_index]; } } elseif ($type == "complete") { { $current[$tag] = $result; $repeated_tag_index[$tag . '_' . $level] = 1; if ($priority == 'tag' and $attributes_data) $current[$tag . '_attr'] = $attributes_data; } else { { $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; if ($priority == 'tag' and $get_attributes and $attributes_data) { $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; } $repeated_tag_index[$tag . '_' . $level]++; } else { $current[$tag], $result ); $repeated_tag_index[$tag . '_' . $level] = 1; if ($priority == 'tag' and $get_attributes) { { $current[$tag]['0_attr'] = $current[$tag . '_attr']; } if ($attributes_data) { $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; } } $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken } } } elseif ($type == 'close') { $current = & $parent[$level -1]; } } return ($xml_array); }
So now lets change the above XML to look like this
$xml= '<persons> <person n="2"> <name>Jaspreet Chahal</name> <age id="1">33</age> <city>Melbourne</city> </person> <person> <name>Yuvraaj Chahal</name> <age>2</age> <city>Adelaide</city> </person> </persons>';
and then pass the above XML to xml2array() shown below
The output will look like this
( ( ( ( [name] => Jaspreet Chahal [age] => 33 ( [id] => 1 ) [city] => Melbourne ) ( [name] => Yuvraaj Chahal [age] => 2 [city] => Adelaide ) ( [n] => 2 ) ) ) )
I hope this helps because it helped me immensely.
Cheers





JC WordPress Coupon Revealer Plugin Pro License
Australian Street Names with City, State and Display Names only, Single Server License
You’re still setting $contents to an empty value at the beginning of the function
remove the $contents = “”;
true!
I think I shouldn’t be editing any posts when I am high on caffeine
thanks again James and Merry Christmas.
I just wanted to point out that your modifications to the xml2array function is busted.
It’s impossible that your modified code will work. Upon running the function, you immediately empty the $contents variable (so the XML to parse is now completely gone) and you never actually create the parser.
Currently your code has the following:
$contents = “”;
if (!function_exists(‘xml_parser_create’))
{
return array ();
}
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, “UTF-8″);
What you need is this:
if (!function_exists(‘xml_parser_create’))
{
return array ();
}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, “UTF-8″);
Thanks James,
What I was using was a little bit different to what I posted. So thanks for revising it.
I have made the modifications and I think it should work fine.
The code was taken from PHP.net and I think I should just have copy pasted the code I am using in my project here
Thanks once again.
Cheers.