/XML.php

https://github.com/davissuperman/tuangou · PHP · 186 lines · 163 code · 18 blank · 5 comment · 23 complexity · dcc10703fcbd4ec0107d12008c322482 MD5 · raw file

  1. <?php
  2. class XML {
  3. protected $_file = null;
  4. protected $_tableName = null;
  5. protected $_tableItem = array ();
  6. function __construct($path, $couponName) {
  7. $file = $this->generateXml ( $path, $couponName );
  8. $this->saveXml ( $file );
  9. }
  10. function generateXml($path, $couponName) {
  11. $date = date ( 'Ymd' );
  12. $siteName = $couponName . $date;
  13. $tempfilename = 'xml/' . $siteName . '.xml';
  14. $this->_tableName = $siteName;
  15. if (! file_exists ( $tempfilename )) {
  16. $fopen = fopen ( $tempfilename, 'w+' ); //新建文件命令
  17. fclose ( $fopen );
  18. //$srcurl = 'http://open.client.lashou.com/api/detail/city/%E4%B8%8A%E6%B5%B7/p/1';
  19. $srcurl = $path;
  20. $curl = curl_init ();
  21. curl_setopt ( $curl, CURLOPT_URL, $srcurl );
  22. curl_setopt ( $curl, CURLOPT_HEADER, 0 );
  23. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
  24. $htmldata = curl_exec ( $curl );
  25. $htmldata = preg_replace ( '/&/', '&amp;', $htmldata );
  26. curl_close ( $curl );
  27. // $htmldata = file_get_contents ( $srcurl );
  28. $tempfile = fopen ( $tempfilename, 'w+' );
  29. if (! $tempfile) {
  30. echo ("<P>Unable to open temporary file " . "($tempfilename) for writing. Static page " . "update aborted!</P>");
  31. exit ();
  32. }
  33. fwrite ( $tempfile, (html_entity_decode ( $htmldata, ENT_QUOTES, 'UTF-8' )) );
  34. // $htmldata = str_replace('&hearts;', '', $htmldata);
  35. fclose ( $tempfile );
  36. }
  37. return $tempfilename;
  38. }
  39. function saveXml($file) {
  40. $tempfilename = $file;
  41. $items_table = array ();
  42. if (file_exists ( $tempfilename )) {
  43. $xml = simplexml_load_file ( $tempfilename, 'SimpleXMLElement', LIBXML_NOCDATA );
  44. $tag = array ();
  45. $parent = array ();
  46. XML::parseXML ( $xml, $parent, $tag );
  47. foreach ( $tag as $key => $item ) {
  48. $value = $parent [$key];
  49. $item_key = trim ( $tag [$key] );
  50. switch ($item_key) {
  51. case 'range' :
  52. $item_key = 'range1';
  53. break;
  54. default :
  55. }
  56. if (is_numeric ( $value )) {
  57. if (( int ) $value > ( int ) strtotime ( "1990-01-01" )) {
  58. $items_table [$item_key] = ' timestamp ';
  59. } else {
  60. $items_table [$item_key] = ' float ';
  61. }
  62. } elseif (is_string ( $value )) {
  63. $items_table [$item_key] = ' varchar (200) CHARACTER SET gb2312 ';
  64. }
  65. }
  66. $this->_tableItem = $items_table;
  67. $this->generateTable ( $items_table );
  68. //save data
  69. $content = array ();
  70. $this->saveXMLTOTable ( $xml, $content, 0 );
  71. // DataBase::dumpNow ( $content );
  72. }
  73. }
  74. function addMark($var) {
  75. return "`" . $var . "`";
  76. }
  77. function saveXMLTOTable($node, &$content = array(), $key) {
  78. $count = 0;
  79. $table = $this->_tableName;
  80. $table_item = $this->_tableItem;
  81. $table_item = array_map(array("XML","addMark"), $table_item);
  82. $keys = array_keys ( $table_item );
  83. $key_str = implode ( ',', $keys );
  84. $question_mark = array ();
  85. foreach ( $keys as $every_key ) {
  86. $question_mark [] = '?';
  87. }
  88. if ($node->count () == 1)
  89. $node = $node->children ();
  90. foreach ( $node->children () as $child_name => $child_node ) {
  91. $insert_sql = "insert into $table ";
  92. $insert_sql .= "(";
  93. $insert_sql .= $key_str . ")values";
  94. $content = array ();
  95. $value_array = array ();
  96. XML::getSubNodeValue ( $child_node, $content );
  97. $count ++;
  98. foreach ( $keys as $every_key ) {
  99. if (array_key_exists ( $every_key, $content )) {
  100. $c = $content [$every_key];
  101. } else {
  102. $c = " ";
  103. }
  104. $value_array [] = addslashes ( $c );
  105. }
  106. $insert_sql .= "(" . implode ( ',', $question_mark ) . ")";
  107. DataBase::$db->Execute ( $insert_sql, $value_array );
  108. }
  109. }
  110. static function getSubNodeValue($node, &$content = array()) {
  111. if ($node->count () >= 1) {
  112. foreach ( $node->children () as $child_name => $child_node ) {
  113. if ($node->count () >= 1) {
  114. XML::getSubNodeValue ( $child_node, $content );
  115. } else {
  116. $content [$child_node->getName ()] = "$child_node";
  117. }
  118. }
  119. } else {
  120. $content [$node->getName ()] = "$node";
  121. }
  122. }
  123. function generateTable($items_table) {
  124. $table = $this->_tableName;
  125. $table_exist = DataBase::getDB ()->getCol ( 'show tables like "' . $table . '"' );
  126. $exists = ! empty ( $table_exist );
  127. if (! $exists) {
  128. $sql = null;
  129. foreach ( $items_table as $item => $set_item ) {
  130. $sql .= "`$item`" . " $set_item ,";
  131. }
  132. $create_sql = 'CREATE TABLE `' . $table . '` (
  133. `id` int(11) NOT NULL AUTO_INCREMENT,' . $sql . '
  134. PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=gbk;';
  135. DataBase::getDB ()->Execute ( $create_sql );
  136. $flush_table = "flush table $table";
  137. DataBase::getDB ()->Execute ( $flush_table );
  138. }
  139. }
  140. static function parseXML($node, &$parent = array(), &$tag = array()) {
  141. $only_child = true;
  142. if ($node->count () >= 1)
  143. $only_child = FALSE;
  144. $node_name = $node->getName ();
  145. if ($only_child) {
  146. $content = "$node";
  147. $parent [] = $content;
  148. if (! in_array ( $child_node->getName (), $tag )) {
  149. $tag [] = $child_node->getName ();
  150. $content = "$child_node";
  151. $parent [] = $content;
  152. }
  153. }
  154. $count = 0;
  155. foreach ( $node->children () as $child_name => $child_node ) {
  156. if ($child_node->count () >= 1)
  157. XML::parseXML ( $child_node, $parent, $tag );
  158. else {
  159. if (! in_array ( $child_node->getName (), $tag )) {
  160. $tag [] = $child_node->getName ();
  161. $content = "$child_node";
  162. $parent [] = $content;
  163. }
  164. }
  165. $count ++;
  166. }
  167. }
  168. }