hasChildNodes()) { foreach ($node->childNodes as $children) { if ($children->nodeType == XML_ELEMENT_NODE) return TRUE; } } return FALSE; } /* * $array = xml_to_array_parser($parent, $counter = -1, $index = 0, &$array = FALSE, $first_level = TRUE) * * This is the core: parses the XML file and initializes the array */ function xml_to_array_parser($parent, $counter = -1, $index = 0, &$array = FALSE, $first_level = TRUE) { // Extract the childrens from the parent node foreach($parent->childNodes as $children) { if($counter != -1) { if($counter > 0) --$counter; else break; } // Checks for children's attributes if($children->hasAttributes()) foreach($children->attributes as $attribute) $array[$index][$attribute->name] = $attribute->value; // Insert a valid element into the array if($children->nodeName[0] != "#" && !has_childs($children) && !$first_level) $array[$index][$children->nodeName] = $children->nodeValue; // Checks if the children is a also a parent if(has_childs($children)) xml_to_array_parser($children, $counter, $index, $array, FALSE); // The real parent only can creates a new table if($first_level) ++$index; } // Return the table if($first_level) return $array; } /* * $array = xml_parser($xml_path, $counter = -1) * * Simple function that returns back the array * associated to the XML file. * * If counter is > 0, the function will reads * $counter records from the XML file. */ function xml_parser($xml_path, $counter = -1) { // Creates an object of the DOMDocument class $dom = new DOMDocument(); // Ignore the blank spaces in the XML file $dom->preserveWhiteSpace = FALSE; // Loads the XML file @$dom->load($xml_path) or die("Invalid XML file!"); // Root of the XML file $root = $dom->documentElement; // Returns the array or FALSE return ($root->hasChildNodes()) ? xml_to_array_parser($root, $counter) : FALSE; } /********* SAMPLE CODE *********/ $list = xml_parser("file.xml"); foreach($list as $array) { echo "