doulu1914 2014-09-15 09:38
浏览 50
已采纳

如何编辑我的代码以从XML的开头保存到mySQL?

I have this XML feed below I am trying to import into MySQL for all the products. For example, inside the table XML_FEED I want something like

shop        -   product_id - product_name - product_link - .......
mywebstore  -   322233     - MadBiker 600 - .........
mywebstore  -   324633     - Samsung S4 - .........

The code until now it works only if the XML begins from <products> and not from <mywebstore>

How to change my code to do this ?

$xml = simplexml_load_file("test.xml");
foreach($xml->product as $product)
{
    $columns = array();
    $data = array();
    foreach($product->children() as $child)
    {
        echo $child->getName() . ": " . $child . "<br />";
        $columns[] = $child->getName();
        $data[] = mysql_real_escape_string((string)$child);
    }
    $col = '`'. implode('`,`',$columns) .'`';
    $val = "'". implode("','",$data)."'";
    $query = "INSERT INTO XML_FEED ($col) VALUES ($val)";
    echo $query;

    mysql_query($query);
}

Here is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<mywebstore>
   <created_at>2010-04-08 12:32</created_at>
   <products>
      <product>
        <id>322233</id>
        <name><![CDATA[MadBiker 600]]></name>
        <link><![CDATA[http://www.mywebstore.co.uk/product/322233]]></link>
        <image><![CDATA[http://www.mywebstore.co.uk/product/322233.jpg]]></image>
        <category><![CDATA[Outdor > Extreme Sports]]></category>
        <price_with_vat>322.33</price_with_vat>
      </product>
      ...
      ...
      ...
   </products>
</mywebstore>
  • 写回答

6条回答 默认 最新

  • dongma7725 2014-12-29 17:52
    关注

    This should work for you:

    <?php
    
        //Xml stuff
        $xml = simplexml_load_file("file.xml");
    
        //Database stuff
        $hostname = "localhost";
        $username = "root";
        $password = "";
    
        try {
            //DB Connection
            $dbh = new PDO("mysql:host=$hostname;dbname=dbname", $username, $password);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo "Connected to Database<br/>";
    
    
            foreach($xml->products->product as $data) {
                $sql = "INSERT INTO XML_FEED (shop, product_id, product_name, product_link, product_image, product_category, product_price_with_vat)
                    VALUES (:SHOP, :ID, :NAME, :LINK, :IMAGE, :CATEGORY, :PRICE)";
                $stmt = $dbh->prepare($sql);
    
                $params = array(
                    "SHOP" => $xml->getName(),
                    "ID" => $data->id ,
                    "NAME" => $data->name,
                    "LINK" => $data->link,
                    "IMAGE" => $data->image,
                    "CATEGORY" => $data->category,
                    "PRICE" => $data->price_with_vat
                );
                $stmt->execute($params);
    
            }
    
            //Close Connection
            $dbh = null;
    
        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    
    ?>
    

    Site Note:

    Add error reporting to the top of your file(s) which will help during production testing.

    <?php
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
    ?>
    

    Also if you want to show/see the data in html you can use this:

    <?php
    
        //Xml stuff
        $xml = simplexml_load_file("file.xml");
    
        echo "<table border='1'>";
    
        echo "<tr>
                <td>Shop</td>
                <td>Product ID</td>
                <td>Product Name</td>
                <td>Product Link</td>
                <td>Product Image</td>
                <td>Product Category</td>
                <td>Product Price with vat</td>
            </tr>";
    
        foreach($xml->products->product as $data) {
            echo "<tr>
                <td>" . $xml->getName() . "</td>
                <td>" . $data->id . "</td>
                <td>" . $data->name . "</td>
                <td>" . $data->link . "</td>
                <td>" . $data->image . "</td>
                <td>" . $data->category . "</td>
                <td>" . $data->price_with_vat. "</td>
            </tr>";
        }
    
        echo "</table>";
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本