【问题标题】:Parse XML file and insert results in MySQL解析 XML 文件并在 MySQL 中插入结果
【发布时间】:2012-11-22 14:47:24
【问题描述】:

我正在尝试将 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 货币子项的数值)并将它们打印在屏幕上,但我很难将它们引入数据库。任何帮助将不胜感激...

【问题讨论】:

  • 有一次我想以某种方式自动化这个,所以我需要让脚本以预定义的时间间隔运行......

标签: php mysql xml foreach


【解决方案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

【讨论】:

    猜你喜欢
    • 2012-02-07
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 2018-01-02
    相关资源
    最近更新 更多