468,242 Members | 1,591 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

turn xml into 2 dimensional array

Well, www.php.net seems utterly crashed. Does anyone know how to get
an XML stream and turn it into a 2 dimensional array?
Jul 16 '05 #1
2 6341
With total disregard for any kind of safety measures
lk******@geocities.com (lawrence) leapt forth and uttered:
Well, www.php.net seems utterly crashed. Does anyone know how to
get an XML stream and turn it into a 2 dimensional array?


Here's a function I use to do that:

function fetch_xml($xml){

if(is_file($xml)){
$xml_data = file_get_contents($xml);
}
else{
$xml_data = $xml;
}
$parser = xml_parser_create();
@xml_parse_into_struct($parser, $xml_data, $assoc_arr, $idx_arr);
@xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
@xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
$root_tag = $assoc_arr[0]['tag'];
$base_tag = strtolower($assoc_arr[1]['tag']);
$i = 0;
foreach($assoc_arr as $key => $element){
if($element['tag'] != $root_tag){
if(!preg_match('/^\s+$/', $element['value'])){
$tag = strtolower($element['tag']);
$items[$i][$tag] = $element['value'];
if($tag == $base_tag){
$i++;
}
}
elseif(isset($element['attributes'])){
$items[$i]['id'] = $element['attributes']['ID'];
}
}
}

return $items;
}

--
There is no signature.....
Jul 16 '05 #2
Phil Roberts <ph*****@HOLYflatnetSHIT.net> wrote in message news:<Xn*************************@216.196.97.132>. ..
With total disregard for any kind of safety measures
lk******@geocities.com (lawrence) leapt forth and uttered:
Well, www.php.net seems utterly crashed. Does anyone know how to
get an XML stream and turn it into a 2 dimensional array?


Here's a function I use to do that:

function fetch_xml($xml){

if(is_file($xml)){
$xml_data = file_get_contents($xml);
}
else{
$xml_data = $xml;
}
$parser = xml_parser_create();
@xml_parse_into_struct($parser, $xml_data, $assoc_arr, $idx_arr);
@xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
@xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
$root_tag = $assoc_arr[0]['tag'];
$base_tag = strtolower($assoc_arr[1]['tag']);
$i = 0;
foreach($assoc_arr as $key => $element){
if($element['tag'] != $root_tag){
if(!preg_match('/^\s+$/', $element['value'])){
$tag = strtolower($element['tag']);
$items[$i][$tag] = $element['value'];
if($tag == $base_tag){
$i++;
}
}
elseif(isset($element['attributes'])){
$items[$i]['id'] = $element['attributes']['ID'];
}
}
}

return $items;
}


Perfect. Thanks.
Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Ruben | last post: by
16 posts views Thread by rguti | last post: by
60 posts views Thread by Peter Olcott | last post: by
22 posts views Thread by spam.noam | last post: by
6 posts views Thread by fniles | last post: by
272 posts views Thread by Peter Olcott | last post: by
5 posts views Thread by nelly0 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.