如何将XML文件插入数据库php

时间:2016-10-25 00:26:57

标签: php xml database multidimensional-array

上面的链接与我的问题OMG无关 我想将每个节点分配给一个变量,这样我就可以控制数据将它们插入到数据库中。

XML文件示例

<?xml version="1.0"?>
    <catalog>
       <book>
            <took>
                <cl>
                    <shp>
                        <number>
                            <data>
                              <author>jack</author>
                              <title>Midnight Rain</title>
                              <genre>Fantasy</genre>
                              <price>5.95</price>
                              <publish_date>2000-12-16</publish_date>
                              <description>A former architect battles corporate zombies.</description>
                              </data>

                            <data1>
                                <author>kirito</author>
                                <title>Midnight Rain</title>
                                <genre>Fantasy</genre>
                                <price>5.95</price>
                                <publish_date>2000-12-16</publish_date>
                                <description>A former architect battles corporate zombies.</description>
                            </data1>
                        </number>
                    </shp>
                </cl>
            </took>
        </book>
    </catalog>

如您所见,并且具有相同的子标记,但不同的父标记名称

如何将数据拉入变量以将其插入数据库

我几乎阅读了互联网上的所有内容,但所有内容都使用了属性!

-------------已编辑

我尝试将XML转换为数组,这就是结果

Array
(
    [catalog] => Array
        (
            [book] => Array
                (
                    [took] => Array
                        (
                            [cl] => Array
                                (
                                    [shp] => Array
                                        (
                                            [number] => Array
                                                (
                                                    [data] => Array
                                                        (
                                                            [author] => jack
                                                            [title] => Midnight Rain
                                                            [genre] => Fantasy
                                                            [price] => 5.95
                                                            [publish_date] => 2000-12-16
                                                            [description] => A former architect battles corporate zombies.
                                                        )

                                                    [data1] => Array
                                                        (
                                                            [author] => Kirito
                                                            [title] => Midnight Rain
                                                            [genre] => Fantasy
                                                            [price] => 5.95
                                                            [publish_date] => 2000-12-16
                                                            [description] => A former architect battles corporate zombies.
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

我是通过使用此代码

来做到的
function XML2Array(SimpleXMLElement $parent)
{
    $array = array();

    foreach ($parent as $name => $element) {
        ($node = & $array[$name])
            && (1 === count($node) ? $node = array($node) : 1)
            && $node = & $node[];

        $node = $element->count() ? XML2Array($element) : trim($element);
    }

    return $array;
}


$xml   =  simplexml_load_file('lol.xml');
$array = XML2Array($xml);
$array = array($xml->getName() => $array);

此代码用于提取数据

$author_array = array();
array_walk_recursive($array, function($value, $key) {
    if (in_array($key, array("author"))) {
       echo  $author_array[] = $value;
    }
});

但它返回两个作者的问题

jack和kirito

我想显示一个数据取决于关键数据或data1

我该怎么做?

0 个答案:

没有答案