* This software is dual licensed under LGPL v2.1 * and LGPL v3 */ class BArita_XML_Parser { // The incoming XML Feed public $xmlFeed; // The resulting php data array public $dataArray; public function __construct() { } public function toPHPArray($xmlFeed) { $this->xmlFeed = $xmlFeed; $main_parser = xml_parser_create('UTF-8'); // Do not upper case everything xml_parser_set_option($main_parser, XML_OPTION_CASE_FOLDING, 0); // Do not skip any characters in tag names xml_parser_set_option($main_parser, XML_OPTION_SKIP_TAGSTART, 0); // Do not ignore white space xml_parser_set_option($main_parser, XML_OPTION_SKIP_WHITE, 0); // Do a direct parsing xml_parse_into_struct($main_parser, $xmlFeed, $xmlvaluealues, $xml_indexes); // Convert the struct to an array in PHP $this->convertXMLStructToPHPArray($this->dataArray, $xmlvaluealues, $xml_indexes); // Free the resource xml_parser_free($main_parser); } public function convertXMLStructToPHPArray(&$resultArray, &$XMLValuesFromStruct, &$XMLIndexesFromStruct) { $resultArray = array('xml'=>array(), 'id' => array(), 'parent'=>array()); $currentPosition = &$resultArray['xml']; $count = 0; foreach($XMLValuesFromStruct as $key=>$value) { if($value['type'] == 'open') { $element = array('id' => $count, 'tag' => $value['tag']); if (!empty($value['attributes'])) { foreach($value['attributes'] as $name=>$value) { $element['attrib'][$name] = $value; } } if (!empty($value['cdata'])) { $element['cdata'] = $value['cdata']; } if (!empty($value['value'])) { $element['value'] = $value['value']; } if (empty($currentPosition['child'][$value['tag']])) { $currentPosition['child'][$value['tag']] = array(); } if (empty($currentPosition['childOrder'])) { $currentPosition['childOrder'] = array(); } $elementPosition = count($currentPosition['child'][$value['tag']]); $currentPosition['child'][$value['tag']][$elementPosition] = $element; $resultArray['id'][$count] = &$currentPosition['child'][$value['tag']][$elementPosition]; $currentPosition['childOrder'][] = &$currentPosition['child'][$value['tag']][$elementPosition]; $resultArray['parent'][$count] = &$currentPosition; $currentPosition = &$currentPosition['child'][$value['tag']][$elementPosition]; } elseif($value['type'] == 'complete') { $element = array('id' => $count,'tag' => $value['tag']); if (!empty($value['attributes'])) { foreach($value['attributes'] as $name=>$value) { $element['attrib'][$name] = $value; } } if (!empty($value['cdata'])) { $element['cdata'] = $value['cdata']; } if (!empty($value['value'])) { $element['value'] = $value['value']; } if (empty($currentPosition['child'][$value['tag']])) { $currentPosition['child'][$value['tag']] = array(); } if (empty($currentPosition['childOrder'])) { $currentPosition['childOrder'] = array(); } $elementPosition = count($currentPosition['child'][$value['tag']]); $currentPosition['child'][$value['tag']][$elementPosition] = $element; $resultArray['id'][$count] = &$currentPosition['child'][$value['tag']][$elementPosition]; $currentPosition['childOrder'][] = &$currentPosition['child'][$value['tag']][$elementPosition]; $resultArray['parent'][$count] = &$currentPosition; } elseif($value['type'] == 'close') { $currentPosition = &$resultArray['parent'][$currentPosition['id']]; } ++$count; } } public function toXMLFeed($dataArray = NULL) { if (empty($dataArray)) { $dataArray = $this->dataArray['xml']; } return preg_replace('/>[ \n\r]*<', $this->convertPHPArrayToXMLStruct($dataArray, 0)); } public function convertPHPArrayToXMLStruct(&$dataArray, $level) { $xmlResult = array(); if (empty($dataArray['childOrder'])) { return ''; } else { foreach($dataArray['childOrder'] as $row=>$element) { $elementString = ''; if (!empty($elements['attrib'])) { foreach($element['attrib'] as $name=>$value) { $elementString .= ' ' . $name . '="' . $value . '"'; } } $xmlResult[] = '<' . $element['tag'] . $elementString . '>'; if (!empty($dataArray['childOrder'])) { $xmlResult[] = $this->convertPHPArrayToXMLStruct($element, $level + 1); } if (!empty($element['value'])) { $xmlResult[] = str_replace(array('&','<','>','"','\''), array('&','<','>','"','&apos'),$element['value']); } $xmlResult[] = '"; } } return implode('',$xmlResult); } } /* $xml = << This is a real error... This is a test This is a second error... Lots of errors today... This is the last error of all time...>heheh XML; $test = new BArita_XML_Parser(); $test->toPHPArray($xml); print $test->toXMLFeed(); */