解析XML文件并在MySQL中插入结果

时间:2012-12-04 21:11:53

标签: php mysql xml foreach

我正在尝试将XML文件导入MySQL数据库,我在此过程中遇到了一些问题。这是我到目前为止的代码:

function convertXML($node){
    foreach($node->Body->Cube as $child)
        {
            echo $child['date'] .",";
                foreach($child->Rate as $child1)
                {
                    echo $response=$child1 . ",";
                }
            echo "<br>";
                   //mysql_query("INSERT INTO table ('date', 'AED', 'AUD', 'BGN', 'BRL') VALUES $response");
        }
}
$row = convertXML($xml);

这是我的XML文件示例:

<DataSet>
    <Header>
      <PublishingDate>2012-12-04</PublishingDate>
      <MessageType>DR</MessageType>
    </Header>
    <Body>
      <Subject>Reference</Subject>
      <OrigCurrency>RO</OrigCurrency>
       <Cube date="2012-12-03">
          <Rate currency="AED">0.9439</Rate>
          <Rate currency="AUD">3.6275</Rate>
          .....
          <Rate currency="BGN">2.3170</Rate>
       </Cube>
       <Cube date="2012-12-04">
          <Rate currency="AED">0.93129</Rate>
          <Rate currency="AUD">3.3576</Rate>
           ....
          <Rate currency="BGN">2.245/Rate>
       </Cube>
    </Body>
</DataSet>

我可以得到我感兴趣的结果(日期和RAte货币子项的数值)并将它们打印在屏幕上,但我很难将它们引入数据库。任何帮助将不胜感激......

1 个答案:

答案 0 :(得分:1)

你试过LOAD XML LOCAL INFILE ???

mysql> LOAD XML LOCAL INFILE 'address.xml'
    ->   INTO TABLE person
    ->   ROWS IDENTIFIED BY '<person>';

此处提供更多信息:

http://dev.mysql.com/doc/refman/5.5/en/load-xml.html