Xml到sql数据库

时间:2015-01-09 23:04:27

标签: php mysql xml

我试图制作一个php文件将xml导入sql数据库,但我有这个错误 - :

警告:在第18行的/home/morrisse/public_html/xml_to_database.php中为foreach()提供的参数无效

我的代码:

        $url ="http://www.example/example.xml";
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $URL);  //GET THE URL CONTENDS

    $data = curl_exec ($ch);  //execute the curl request

    curl_close($ch);

    $xml = simplexml_load_string($data);
    foreach ($xml -> track as $row) {
                $title = $row -> name;
                $artist = $row -> artist;
                $duration = $row -> duration;
    // performing sql query

    $sql = "INSERT INTO 'test_xml' ('title', 'artist', 'duration')"
                . "VALUES ('$title', '$artist', '$duration')";

    $result = mysql_query($sql);
    if (!result) {
                    echo 'MYSQL ERROR';
                }   else {
                echo 'SUCESSO';
                }
                }

我的xml:

      <lfm status="ok">
    <track>
    <id>602366984</id>
    <name>Barbarism Begins At Home (BBC TV Tube performance)</name>
    <mbid/>
    <url>http://www.last.fm/music/The+Smiths/_/Barbarism+Begins+At+Home+(BBC+TV+Tube+performance)</url>
    <duration>384000</duration>
    <streamable fulltrack="0">0</streamable>
    <listeners>10</listeners>
    <playcount>24</playcount>
    <artist>
    <name>The Smiths</name>
    <mbid>40f5d9e4-2de7-4f2d-ad41-e31a9a9fea27</mbid>
    <url>http://www.last.fm/music/The+Smiths</url>
    </artist>
    <toptags>
    </toptags>
    </track>
    </lfm>

请帮忙吗?

完整代码:

<?php

$url ="http://www.morrisseyradio.com/shoutcastinfo/cache/track.xml";
$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);  //GET THE URL CONTENDS

$data = curl_exec ($ch);  //execute the curl request

curl_close($ch);
$con=mysql_connect("localhost", "", ""); //connect to database
mysql_select_db("morrisse_forum", $con) 
if (!$con) {
    die('Could not connect: ' . mysql_error());
}; // select database


libxml_use_internal_errors(true);
$xml = simplexml_load_string($data);


if ($xml === false) {
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
        echo "XML Error at line $error->line: $error->message\n";
    }
}
 else {
foreach ($xml -> track as $row) {
            $title = $row -> name;
            $artist = $row -> artist;
            $duration = $row -> duration;
// performing sql query

$sql = "INSERT INTO 'test_xml' ('title', 'artist', 'duration')"
            . "VALUES ('$title', '$artist', '$duration')";

$result = mysql_query($sql);
if (!result) {
                echo 'MYSQL ERROR';
            }   else {
            echo 'SUCESSO';
            }
            }
            }

?>

2 个答案:

答案 0 :(得分:1)

对于任何人,答案是: 第一件事: 在行:

curl_setopt($ch, CURLOPT_URL, $URL);

&#34; $ URL&#34;必须低于

第二: 在这一行:

$sql = "INSERT INTO 'test_xml' ('title', 'artist', 'duration')"

&#34;&#39;&#34;必须是&#34;`&#34;

答案 1 :(得分:0)

很明显,您对simplexml_load_string()的调用失败了,因为xml格式不正确。当发生这种情况时,您将返回false而不是可用的xml元素。

我建议你这样做。添加一些错误跟踪代码,如下所示。

libxml_use_internal_errors(true);
$xml = simplexml_load_string($data);

if ($xml === false) {
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
        echo "XML Error at line $error->line: $error->message\n";
    }
}
 else {
     foreach ( $xml->track as $row ) {  /* your code continues here  ... */

您从使用curl的服务器返回的xml显然有问题。此代码将帮助您找出问题所在。