2010-10-24 47 views
1

我被要求解析一个存储为XML文件的简单文件,然后将数据保存到mysql中。使用php解析XML并将其保存到mysql

但是我完全不知道该怎么办,在网上查看后,所有给出的例子似乎对我的问题或者是不正确的解决方案来说太复杂了。 XML文件看起来像这样

games_scores.xml

<gesmes:Letter> 
<gesmes:subject>Reference Scores</gesmes:subject> 
- 
<gesmes:Sender> 
<gesmes:name>Game Information Scores</gesmes:name> 
</gesmes:Sender> 
- 
<Cube> 
- 
<Cube time="2010-10-13"> 
<Cube scores="GameA1" value="1.5803"/> 
<Cube scores="GameA2" value="21.35"/> 
............etc 
<Cube scores="GameA15" value="135"/> 
</Cube> 
</Cube> 
</gesmes:Letter> 

的话,我必须保存它到mysql现有的,这是

CREATE TABLE IF NOT EXISTS `scrore_table` (
    `scrore_id` int(11) NOT NULL auto_increment, 
    `scrore_title` varchar(32) collate utf8_bin NOT NULL default '', 
    `scores` varchar(15) collate utf8_bin NOT NULL default '', 
    `decimal_place` char(1) collate utf8_bin NOT NULL, 
    `value` float(15,8) NOT NULL, 
    `date_updated` datetime NOT NULL default '0000-00-00 00:00:00', 
    PRIMARY KEY (`currency_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; ; 

INSERT INTO `scrore_table` (`scrore_id`, `scrore_title`, `scores`, `decimal_place`, `value`, `date_updated`) VALUES 
(1, 'Game Class A0', 'GameA0', '2', 1.00000000, '2010-04-06 22:00:54'), 
(2, 'Game Class A1', 'GameA1', '2', 1.58030000, '2010-04-06 22:00:54'), 
(3, 'Game Class A2', 'GameA2', '2', 21.3503000, '2010-04-06 22:00:54'), 
..............................etc 
(15, 'Game Class A15', 'GameA15', '2', 135, '2010-04-06 22:00:54'); 

方向看起来这个问题,我按照有PHP脚本:

<?php 
class Scores_Converter { 
    var $xml_file = "http://192.168.1.112/gamescores/games_scores.xml"; 
    var $mysql_host, $mysql_user, $mysql_pass, $mysql_db, $mysql_table; 
    var $scores_values = array(); 

    //Load convertion scores 
    function Scores_Converter($host,$user,$pass,$db,$tb) { 
     $this->mysql_host = $host; 
     $this->mysql_user = $user; 
     $this->mysql_pass = $pass; 
     $this->mysql_db = $db; 
     $this->mysql_table = $tb; 

     $this->checkLastUpdated(); 

     $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass); 
     $rs = mysql_select_db($this->mysql_db,$conn); 
     $sql = "SELECT * FROM ".$this->mysql_table; 
     $rs = mysql_query($sql,$conn); 
     while($row = mysql_fetch_array($rs)) { 
     $this->scores_values[$row['scores']] = $row['value'];   
     } 
    } 

    /* Perform the actual conversion, defaults to 1.00 GameA1 to GameA3 */ 
    function convert($amount=1,$from="GameA1",$to="GameA3",$decimals=2) {  return(number_format(($amount/$this->scores_values[$from])*$this->scores_values[$to],$decimals)); 
    } 

    /* Check to see how long since the data was last updated */ 
    function checkLastUpdated() { 
     $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass); 

     $rs = mysql_select_db($this->mysql_db,$conn); 
     $sql = "SHOW TABLE STATUS FROM ".$this->mysql_db." LIKE '".$this->mysql_table."'"; 
     $rs = mysql_query($sql,$conn); 

     if(mysql_num_rows($rs) == 0) { 
     $this->createTable(); 
     } else { 
     $row = mysql_fetch_array($rs); 
     if(time() > (strtotime($row["Update_time"])+(12*60*60))) { 
      $this->downloadValueScores();   
     } 
     } 
    } 

    /* Download xml file, extract exchange values and save the values in database */ 
    function downloadValueScores() { 
     $scores_domain = substr($this->xml_file,0,strpos($this->xml_file,"/")); 
     $scores_file = substr($this->xml_file,strpos($this->xml_file,"/")); 
     $fp = @fsockopen($scores_domain, 80, $errno, $errstr, 10); 
     if($fp) { 
     $out = "GET ".$scores_file." HTTP/1.1\r\n"; 
     $out .= "Host: ".$scores_domain."\r\n"; 
     $out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5\r\n"; 
     $out .= "Connection: Close\r\n\r\n"; 
     fwrite($fp, $out); 
     while (!feof($fp)) { 
      $buffer .= fgets($fp, 128); 
     } 
     fclose($fp); 

     $pattern = "{<Cube\s*scores='(\w*)'\s*value='([\d\.]*)'/>}is"; 
     preg_match_all($pattern,$buffer,$xml_values); 
     array_shift($xml_values); 

     for($i=0;$i<count($xml_values[0]);$i++) { 
      $exchange_value[$xml_values[0][$i]] = $xml_values[1][$i]; 
     } 

     $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass); 
     $rs = mysql_select_db($this->mysql_db,$conn); 

     foreach($exchange_value as $scores=>$value) { 
      if((is_numeric($value)) && ($value != 0)) { 
       $sql = "SELECT * FROM ".$this->mysql_table." WHERE scores='".$scores."'"; 
       $rs = mysql_query($sql,$conn) or die(mysql_error()); 
       if(mysql_num_rows($rs) > 0) { 
        $sql = "UPDATE ".$this->mysql_table." SET value=".$value." WHERE scores='".$scores."'"; 
       } else { 
        $sql = "INSERT INTO ".$this->mysql_table." VALUES('".$scores."',".$value.")"; 
       } 
       $rs = mysql_query($sql,$conn) or die(mysql_error()); 
      } 
     } 
     } 
    } 

    /* Create the scores table */ 
    function createTable() { 
     $conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pass); 
     $rs = mysql_select_db($this->mysql_db,$conn); 
     $sql = "CREATE TABLE ".$this->mysql_table." (`scrore_id` int(11) NOT NULL, `scrore_title` varchar(32) collate utf8_bin NOT NULL default '', `scrores` char(12) NOT NULL default '', `decimal_place` char(1) collate utf8_bin NOT NULL default '2', `value` float(15,8) NOT NULL,`date_updated` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY(currency)) ENGINE=MyISAM"; 
     $rs = mysql_query($sql,$conn) or die(mysql_error()); 
     $sql = "INSERT INTO ".$this->mysql_table." VALUES(1,'','GameA0','2',1,now())"; 
     $rs = mysql_query($sql,$conn) or die(mysql_error()); 
     $this->downloadValueScores(); 
    } 
} 
?> 
与脚本MySQL表/查询创建样子

则:

INSERT INTO `scrore_table` (`scrore_id`, `scrore_title`, `scrores`, `decimal_place`, `value`, `date_updated`) VALUES 
(1, 'Game Class A0', 'GameA0', '2', 1.00000000, '2010-10-24 00:27:01'); 

但数据库不能自动填入,这似乎功能downloadValueScores()无法正常工作

我试图修复它,我觉得这是我必须走的方向,但我只是不知道。另外我很抱歉,我是新来的PHP。

任何帮助或指针将是巨大的,许多感谢

+0

该代码似乎是PHP4。你可以在你的解决方案中使用PHP5吗? – Gordon 2010-10-24 21:29:14

+0

嗨戈登,感谢您的PHP版本信息。实际上我不知道该代码是否为PHP4,因为我不知道它们的不同,请问我可以告诉我PHP5适合我的代码的样子吗? – user485783 2010-10-25 02:31:56

回答

0

从哪里你得到的信息,该查询看起来像这样:

INSERT INTO `scrore_table` (`scrore_id`, `scrore_title`, `scrores`, `decimal_place`, `value`, `date_updated`) 
    VALUES (1, 'Game Class A0', 'GameA0', '2', 1.00000000, '2010-10-24 00:27:01'); 

当我看功能downLoadValueScores,查询将对此作了:

$sql = "INSERT INTO ".$this->mysql_table." VALUES('".$scores."',".$value.")"; 

...这肯定不会建上面的查询,这只会产生一个错误,因为你只有为一行插入带有6个字段的表格而分配的2个值。

+0

您好Dr. Molle,您说的没错,我得到一个错误:“列数不匹配第1行的值计数”,然后我尝试修复它。 – user485783 2010-10-25 03:23:59

+0

by replace:$ sql =“UPDATE”。$ this-> mysql_table。“SET value =”。$ value。“WHERE scores ='”。$ scores。“'”; – user485783 2010-10-25 03:26:37

+0

with:$ sql =“UPDATE”。$ this-> mysql_table。“SET value =”。$ value。“,date_updated ='now()'WHERE scores ='”。$ scores。“'”; – user485783 2010-10-25 03:27:06

0

Molle,你是对的,太好了。

现在scrore_id已经自动递增,

历史修改为: 在功能CREATETABLE() 变化

$sql = "CREATE TABLE ".$this->mysql_table." (`scrore_id` int(11) NOT NULL, `scrore_title` varchar(32) collate utf8_bin NOT NULL default '', `scrores` char(12) NOT NULL default '', `decimal_place` char(1) collate utf8_bin NOT NULL default '2', `value` float(15,8) NOT NULL,`date_updated` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY(scrore_id)) ENGINE=MyISAM"; 

$sql = "CREATE TABLE ".$this->mysql_table." (`scrore_id` int(11) NOT NULL auto_increment, `scrore_title` varchar(32) collate utf8_bin NOT NULL default '', `scrores` char(12) NOT NULL default '', `decimal_place` char(1) collate utf8_bin NOT NULL default '2', `value` float(15,8) NOT NULL,`date_updated` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY(scrore_id)) ENGINE=MyISAM"; 

AUTO_INCREMENT应该后添加scrore_id` int(11)NOT NULL

则函数downloadValueScores()

变化:

$sql = "INSERT INTO ".$this->mysql_table." VALUES('NULL','','".$scores."','2',".$value.",now())"; 

到:

$sql = "INSERT INTO ".$this->mysql_table." VALUES(NULL,'','".$scores."','2',".$value.",now())"; 

结果我的表/查询scrore_id自动递增,并自动填入。

关于scrore_title它类似于国家代码3个字母和国家的名称,例如,

if USA = 'United State' 
if GBR = 'Great Britain' 
if FRC = 'France' etc.. 

下我的问题

if scrores = 'GameA0' then scrore_title = 'Game Class A0' 
if scrores = 'GameA1' then scrore_title = 'Game Class A1' 
if scrores = 'GameA2' then scrore_title = 'Game Class A2' 

在anothers字若发现scrores = 'GameA0' 然后插入scrore_title = '游戏A0级' 等。

scrores已经从想通games_scores.xml但scrore_title我应该与我自己。有没有想法如何解决这个问题?

非常感谢您的帮助。