kmlParser

The XML Google Maps kmlParser class.

Defined (1)

The class is defined in the following location(s).

/xmlgooglemaps_kmlParser.php  
  1. class kmlParser{ 
  2. var $xml_obj = null; 
  3. var $output = array(); 
  4. var $coordinates = array(); 
  5. var $coordinatescount = 0; 
  6. var $currentdata=null; 
  7. var $currentarray=array(); 
  8. var $readingdata=0; 
  9.  
  10. function parse($path) { 
  11. $this->xml_obj = xml_parser_create(); 
  12. xml_set_object($this->xml_obj, $this); 
  13. xml_set_character_data_handler($this->xml_obj, 'dataHandler'); 
  14. xml_set_element_handler($this->xml_obj, "startHandler", "endHandler"); 
  15.  
  16. $old = error_reporting(1); 
  17. if (!($fp = fopen($path, "r"))) { 
  18. xml_parser_free($this->xml_obj); 
  19. error_reporting($old); 
  20. return false; 
  21. } else { 
  22. error_reporting($old); 
  23.  
  24. $filecontent = ""; 
  25. while ($data = fread($fp, 4096)) { 
  26. $filecontent .= $data; 
  27. if (!xml_parse($this->xml_obj, $filecontent, feof($fp))) { 
  28. xml_parser_free($this->xml_obj); 
  29. return false; 
  30. xml_parser_free($this->xml_obj); 
  31.  
  32. return true; 
  33.  
  34. function startHandler($parser, $name, $attribs) { 
  35. switch ($name) { 
  36. case "COORDINATES": 
  37. $this->currentdata=null; 
  38. $this->currentdataarray=array(); 
  39. $this->readingdata=1;  
  40. break; 
  41. default: 
  42. //echo $name."<br>"; 
  43. break; 
  44.  
  45. function dataHandler($parser, $data) { 
  46. if ($this->readingdata) { 
  47. $this->currentdata = $data; 
  48. $this->currentdataarray[$this->readingdata] = trim($data); 
  49. $this->readingdata++; 
  50.  
  51. function endHandler($parser, $name) { 
  52. $this->readingdata=0; 
  53. switch ($name) { 
  54. case "COORDINATES":  
  55. for ($i=1; $i<=count($this->currentdataarray);$i++) { 
  56. preg_match_all('/([0-9\-]+?.[0-9]+?), /', $this->currentdataarray[$i], $found); 
  57. if ((count($found) == 2)) { 
  58. for ($j; $j<count($found[1]); $j+=2) { 
  59. $this->output["coordinates"][$this->coordinatescount]["lon"] = $found[1][$j+0]; 
  60. $this->output["coordinates"][$this->coordinatescount]["lat"] = $found[1][$j+1]; 
  61. $this->coordinatescount++; 
  62. break; 
  63. default: 
  64. break; 
  65.  
  66.