Location: PHPKode > scripts > FuseBox Manager > fusebox-manager/FuseBoxXMLParser.class.php
<?
/*
- author: Skakunov Alexander [hide@address.com]
- filename: FuseBoxXMLParser.class.php
- date: 28.05.2007
- description: FuseBoxXMLParser parses an XML given and creates to convenient PHP arrays: plain $arr_circiuts and nested $arr_structure
*/

class FuseBoxXMLParser
{
  protected $arr_circiuts;
  protected $arr_structure;
  protected $oXML;
  public $project_name;
  
  public function FuseBoxXMLParser($xml)
  {
    if( !function_exists('simplexml_load_string') )
    {
      throw new Exception('No SimpleXML package available. Please add its support.');
    }
    $this->arr_circiuts = array();
    $this->arr_structure = array();
    $this->oXML = simplexml_load_string($xml);
    if(false === $this->oXML)
    {
      throw new Exception('Cannot parse XML provided. Check your data.');
    }
    $bubble = $this->get_top_node();
    $this->process_xml($bubble, $this->arr_structure);
    $this->project_name = $this->get_project_name();
    //new dBug($this->project_name);
  }
  
  protected function process_xml($a_bubble, &$arr, $name="")
  {
    if( !is_object($a_bubble))
    {
      throw new Exception('Wrong XML format');
    }
    
    foreach($this->get_next_node($a_bubble) as $bubble) //???
    {
      
      $text = $this->get_node_name($bubble);
      if( $this->node_has_children($bubble) ) // a circiut
      {
        $this->arr_circiuts [ $text ] = array();
        $arr[ $text ] = array();
        $this->process_xml($bubble, $arr[ $text ], $text);
      }
      else // a fuseaction
      {
        $this->arr_circiuts[ $name ][] = $text;
        $arr[] = $text;
      }
    }
  }

  public function get_structure()
  {
    return $this->arr_structure;
  }

  public function get_circuits()
  {
    return $this->arr_circiuts;
  }
  
  public function get_circiut($circiut)
  {
    return $this->arr_circiuts[$circiut];
  }

  public function get_project_name()
  {
    $bubble = $this->get_top_node();
    return (string)$bubble[0]['text'];
  }
  
  public function get_top_node()
  {
    return $this->oXML->children();
  }
  
  public function get_next_node(&$bubble)
  {
    return $bubble->children()->children();
  }
  
  public function get_node_name($bubble)
  {
    return htmlspecialchars((string)$bubble['text']);
  }
  
  public function node_has_children($bubble)
  {
    return (string)$bubble['haschildren']=='true';
  }
  
}
?>
Return current item: FuseBox Manager