I had a problem were i couldn’t get my xml feeds into an array.I have an easy xml file in some menu items. Now, I want that xmlnodes to be utilized to build a menu. Could you guys help me out for the same please ?
Printable View
I had a problem were i couldn’t get my xml feeds into an array.I have an easy xml file in some menu items. Now, I want that xmlnodes to be utilized to build a menu. Could you guys help me out for the same please ?
You can use this script for the same :
Quote:
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, OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, 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;
}
$items = fetch_xml('articles.xml');
echo '<pre>';
print_r($items);
echo '</pre>';
This can help you to use use xmlnodes to build a menu:
Quote:
var my_xml = new XML();
my_xml.ignoreWhite = true;
my_xml.onLoad = function(success) {
if (success) {
var myArray = my_xml.firstChild.childNodes;
// your xml file is now an array
} else {
trace("XML_Error Loading ");
}
};
my_xml.load("xmlFile.xml");
Its really great, however it seems to attibutefunctie miss. When you load the xml in which the father node has attributes and child Nodes of the child Nodes in series will, however, as only nodes with attributes is the attributes set in the series. It would be great if it somehow did both.