note 57975 modified in function.xml-parse-into-struct by sniper
| From: | sniper@php.net | Date: | Mon, 28 Nov 2005 13:09:12 +0000 |
| Subject: | note 57975 modified in function.xml-parse-into-struct by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-99239@lists.php.net to get a copy of this message | ||
<?
/*
|
| _xml2array - another abstraction layer on xml_parse_into_struct
| that returns a nice nested array.
|
| @param: $xml is a string containing a full xml document
|
| returns: a nested php array that looks like this:
|
| array
| (
| [_name] => the name of the tag
| [_attributes] => an array of 'attribute'=>'value'
combos
| [_value] => the text contents of the node
| [_children] => an array of these arrays, one for each node.
| )
|
| notes: thanks to 'jeffg at activestate dot com' who inspired
| me to essentially re-write his example code from php.net
|
| me: Kieran Huggins < kieran[at]kieran[dot]ca >
|
*/
function _xml2array($xml){
global $keys;
global $level;
if(!is_array($xml)){ // init on first run
$raw_xml = $xml;
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $raw_xml, $xml, $idx);
xml_parser_free($p);
}
for($i=0;$i<count($xml,1);$i++){
// set the current level
$level = $xml[$i]['level'];
if($level<1)break;
// mark this level's tag in the array
$keys[$level] = '['.$i.']';
// if we've come down a level, sort output and destroy the upper level
if(count($keys)>$level) unset($keys[count($keys)]);
// ignore close tags, they're useless
if($xml[$i]['type']=="open" ||
$xml[$i]['type']=="complete"){
// build the evalstring
$e = '$output'.implode('[\'_children\']',$keys);
// set the tag name
eval($e.'[\'_name\'] = $xml[$i][\'tag\'];');
// set the attributes
if($xml[$i]['attributes']){
eval($e.'[\'_attributes\'] = $xml[$i][\'attributes\'];');
}
// set the value
if($xml[$i]['value']){
eval($e.'[\'_value\'] = trim($xml[$i][\'value\']);');
}
}
}
return $output;
}
?>
--was--
My previous function was faulty, this one seems to do the trick though.
Cheers, Kieran
<?
/*
|
| _xml2array - another abstraction layer on xml_parse_into_struct
| that returns a nice nested array.
|
| @param: $xml is a string containing a full xml document
|
| returns: a nested php array that looks like this:
|
| array
| (
| [_name] => the name of the tag
| [_attributes] => an array of 'attribute'=>'value'
combos
| [_value] => the text contents of the node
| [_children] => an array of these arrays, one for each node.
| )
|
| notes: thanks to 'jeffg at activestate dot com' who inspired
| me to essentially re-write his example code from php.net
|
| me: Kieran Huggins < kieran[at]kieran[dot]ca >
|
*/
function _xml2array($xml){
global $keys;
global $level;
if(!is_array($xml)){ // init on first run
$raw_xml = $xml;
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $raw_xml, $xml, $idx);
xml_parser_free($p);
}
for($i=0;$i<count($xml,1);$i++){
// set the current level
$level = $xml[$i]['level'];
if($level<1)break;
// mark this level's tag in the array
$keys[$level] = '['.$i.']';
// if we've come down a level, sort output and destroy the upper level
if(count($keys)>$level) unset($keys[count($keys)]);
// ignore close tags, they're useless
if($xml[$i]['type']=="open" ||
$xml[$i]['type']=="complete"){
// build the evalstring
$e = '$output'.implode('[\'_children\']',$keys);
// set the tag name
eval($e.'[\'_name\'] = $xml[$i][\'tag\'];');
// set the attributes
if($xml[$i]['attributes']){
eval($e.'[\'_attributes\'] = $xml[$i][\'attributes\'];');
}
// set the value
if($xml[$i]['value']){
eval($e.'[\'_value\'] = trim($xml[$i][\'value\']);');
}
}
}
return $output;
}
?>
http://php.net/manual/en/function.xml-parse-into-struct.php