【问题标题】:Extracting XML data into MySQL将 XML 数据提取到 MySQL
【发布时间】:2014-06-28 23:59:30
【问题描述】:

花费了无数个小时试图弄清楚这一点,但无济于事。我得到了一大堆 XML 文件,理想情况下正在寻找将事务直接导入数据库的方法,因此我不必手动执行。

我已经设法得到以下结果,但不知道如何分解产品并使用相同的剩余详细信息将它们作为单独的交易分配给客户。

这是我迄今为止尝试和管理的:

////Daily XML I get sent in this format
<trans>
    <custtrans>
        <cust>564</cust>
        <cust_name>John David</cust_name>
        <product>P1,P2,P3,P4</product>
        <internal>Yes</internal>
    </custtrans>
    <custtrans>
        <cust>877</cust>
        <cust_name>James Harris</cust_name>
        <product>P2</product>
        <internal>No</internal>
    </custtrans>
</trans>

////I'd Like the transactions to be recorded in mysql like this
cust        |cust_name      |product    |internal
564         |John David     |P1         |Yes
564         |John David     |P2         |Yes
564         |John David     |P3         |Yes
564         |John David     |P4         |Yes
877         |James Harris   |P2         |No

////This is how it is being inserted which I do NOT WANT
cust        |cust_name      |product    |internal
564         |John David     |P1,P2,P3,P4|Yes
877         |James Harris   |P2         |No

////my PHP insert statement into database
$db = new PDO($dsn, $username, $password, $options);
$xml = simplexml_load_file('http://xml.com');

foreach ($xml as $insert)
{
        try {
            $stmt = $db->prepare('INSERT INTO customers (cust,cust_name,product,internal) 
            VALUES (:cust,:cust_name,:product,:internal)');
            $stmt->execute(array(
                ':cust' => $insert ->cust,
                ':cust_name' => $insert ->cust_name,
                ':product' => $insert ->product,
                ':internal' => $insert ->internal,
            ));

        //else catch the exception and show the error.
        } catch(PDOException $e) {
            $error[] = $e->getMessage();
        }

}

【问题讨论】:

    标签: php mysql xml


    【解决方案1】:

    您可以将包含以逗号分隔的产品的字符串转换为产品数组,例如使用 PHP 中的explode() 函数。

    然后简单地遍历数组中的所有产品,插入每个产品。

    例子:

    foreach ($xml as $insert)
    {
        $productNamesSeparatedWithCommas = "P1,P2,P3,P4";
    
        $productNamesArray = explode(",", $productNamesSeparatedWithCommas);
    
        foreach ($productNamesArray as $singleProduct)
        {
            try {
                $stmt = $db->prepare('INSERT INTO customers (cust,cust_name,product,internal) 
                VALUES (:cust,:cust_name,:product,:internal)');
                $stmt->execute(array(
                    ':cust' => $insert ->cust,
                    ':cust_name' => $insert ->cust_name,
                    ':product' => $singleProduct,
                    ':internal' => $insert ->internal,
                ));
    
            //else catch the exception and show the error.
            } catch(PDOException $e) {
                $error[] = $e->getMessage();
            }
        }
    
    }
    

    【讨论】:

    • 现在要试试这个,但我可以看到你明确指定了产品范围。上面的产品代码是一个例子。在现实生活中,我会拥有各种不同名称的产品。你怎么能改变产品领域可以是任何东西?
    • 这只是为了说清楚。您可以更改以下内容:$productNamesSeparatedWithCommas = $insert-&gt;product;然后它应该可以工作。
    • 啊啊啊太棒了!我希望我有你的大脑。工作得很好,尝试了一个有更多交易的。工作也很完美。非常感谢您的 +1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    相关资源
    最近更新 更多