| 
<?
/*
 - author: Skakunov Alexander [[email protected]]
 - filename: FuseBoxXMLParser.class.DrevoXMLParser
 - date: 04.05.08
 - description: DrevoXMLParser parses an XML imported from Genery software Drevo application and creates a convenient PHP array of persons in it
 */
 
 class DrevoXMLParser
 {
 protected $arr_persons;
 protected $oXML;
 
 public function DrevoXMLParser($xml)
 {
 //new dBug( $xml, 'xml');
 if( !function_exists('simplexml_load_string') )
 {
 throw new Exception('No SimpleXML package available. Please add its support.');
 }
 $this->arr_persons = array();
 $this->oXML = simplexml_load_string($xml);
 if(false === $this->oXML)
 {
 throw new Exception('Cannot parse XML provided. Check your data.');
 }
 $this->process_xml();
 //new dBug( $this->arr_persons);
 }
 
 protected function process_xml()
 {
 foreach( $this->oXML->Pers->r as $person)
 {
 $id = (string)$person['id'];
 $fields = array( 'bfdate', 'dfdate', 'fn', 'mn', 'sn', 'fullname', 'lifespan', 'sex' );
 foreach( $fields as $field )
 {
 $this->arr_persons[ $id ][$field] = (string)$person->{$field}; //arr[id]=data
 }
 $this->clean_date( $this->arr_persons[ $id ]['bfdate'] );
 $this->clean_date( $this->arr_persons[ $id ]['dfdate'] );
 if( empty( $this->arr_persons[ $id ]['bfdate'] ) && empty( $this->arr_persons[ $id ]['dfdate'] ) ) //no dates, delete this guy
 {
 unset( $this->arr_persons[ $id ] );
 }
 }
 }
 
 public function get_persons()
 {
 return $this->arr_persons;
 }
 
 protected function clean_date(&$str)
 {
 preg_match( '/(\d{2}\.\d{2}\.\d{4})/', $str, $match);
 $str = ( empty($match) ? '' : $match[1] );
 }
 
 }
 
 |