I can't find anything within native PHP to do this: I have to take an XML file and parse it into, not an array proper, but an array of objects.
I don't have a clue as to understanding nodes or blah blah blah so I'm converting it using xml_parse_into_struct() into a 3-dim array and parsing that 3-dim array into an array of objects.
But it doesn't work. Here is my code:
function &generateResultFromXML($willRetrieveDisplay = false, $willOrder = false) { // STATIC OBJECT ARRAY METHOD
global $basePath;
$this->clearResult(); // CLEAR PREVIOUS LATENT RESULTSET OBJECT
$subSection = $this->getSubSection();
$fileID = @fopen("$basePath/xml/${subSection}.xml", 'r');
if (!$fileID) {
$this->dbAP->isSuccessful = false;
$this->setErrorArray(array('action' => 'Could not retrieve XML file for result generating'));
} else {
$xmlContents = @fread($fileID, filesize("$basePath/xml/${subSection}.xml"));
@fclose($fileID);
}
if ($this->dbAP->isSuccessful) {
$parser = @xml_parser_create();
@xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
@xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
@xml_parse_into_struct($parser, $xmlContents, $xmlArray, $tags);
@xml_parser_free($parser);
}
if ($this->dbAP->isSuccessful && is_array($xmlArray) && @sizeof($xmlArray) > 0) {
// NOW YOU MUST LOOP THROUGH $xmlArray AND CONVERT IT INTO AN OBJECT ARRAY RESULTSET
$result = array();
$objKeyArray = @array_keys($xmlArray[1]['attributes']);
for ($i = 1; $i < sizeof($xmlArray) - 1; $i++) {
array_walk(&$result,
create_function('$a = 0', 'global $objKeyArray; if ($result[$a]) $a++; foreach ($objKeyArray as $field) $a->$field = $xmlArray[$i][\'attributes\'][$field];'),
$xmlArray);
@reset($objKeyArray);
}
}
if ($this->dbAP->isSuccessful) return $result;
}
Help appreciated!
Thanx
Phil