如何将xml文件存储到mysql数据库并检索它?

时间:2009-07-14 08:31:20

标签: php html xml

我已从以下代码自动生成XML文件。

if (isset($_POST["song"])&& $_POST['song'] != "") {
    $song = $_POST["song"];
} else {
    $song = array();
}
$dom = new DOMDocument("1.0");
// display document in browser as plain text 
// for readability purposes

// create root element
$root = $dom->createElement("playlist");
$dom->appendChild($root);
$root->setAttribute('version', "1");
$root->setAttribute('xmlns', "http://xspf.org/ns/0/");
$rootnext = $dom->createElement("trackList");
$root->appendChild($rootnext);
foreach ($song as $counter) {
    $tokens = ",";
    $tokenized = strtok($counter, $tokens);
    // create child element
    $song = $dom->createElement("track");
    $rootnext->appendChild($song);
    $song1 = $dom->createElement("creator");
    $song->appendChild($song1);
    $text = $dom->createTextNode("www.musicking.in");
    $song1->appendChild($text); 
    $song1 = $dom->createElement("title");
    $song->appendChild($song1);
    // create text node
    $text = $dom->createTextNode($tokenized);
    $song1->appendChild($text); 
    $tokenized = strtok($tokens);
    $song1 = $dom->createElement("location");
    $song->appendChild($song1);
    $text = $dom->createTextNode($tokenized);
    $song1->appendChild($text); 
}
$dom->save("playlist.xml");

实际上,在生成XML文件后,它存储在根目录中。

请告诉我如何将生成的XML文件存储到MySQL数据库中?

生成XML文件后,我必须调用该文件。我使用下面的代码来打电话:

<object data="musicplayer.swf?autostart=true&playlist=playlist.xml" type="application/x-shockwave-flash" width="400" height="300">
    <param name="movie" value="musicplayer.swf?autostart=true&playlist=playlist.xml"/>
</object>

请告诉我如何从mysql数据库中存储和检索这个playlist.xml文件?

4 个答案:

答案 0 :(得分:2)

使用DOMDocument::saveXML()获取xml文件的字符串表示形式,并将此字符串插入数据库中的approrpiate列。根据数据的大小,使用类型TINYBLOB(2 ^ 8字节),BLOB(2 ^ 16字节),MEDIUMBLOB(2 ^ 24字节)或{{ 1}}(2 ^ 32字节)(如果在您选择的RDBMS中使用MySQL或适当的数据类型)。

答案 1 :(得分:0)

使用DOMDocument->saveXML method代替将XML文档作为字符串。

答案 2 :(得分:0)

<?php 
if(isset($_POST["song"])&& $_POST['song'] != "") 
    {
        $song = $_POST["song"];
    }
    else {$song=array();} 
$dom = new DOMDocument("1.0");
// display document in browser as plain text 
// for readability purposes

// create root element
$root = $dom->createElement("playlist");
$dom->appendChild($root);
$root->setAttribute('version', "1");
$root->setAttribute('xmlns', "http://xspf.org/ns/0/");
$rootnext = $dom->createElement("trackList");
$root->appendChild($rootnext);
foreach ($song as $counter) {
    $tokens = ",";
    $tokenized = strtok($counter, $tokens);
// create child element

$song = $dom->createElement("track");
$rootnext->appendChild($song);
$song1 = $dom->createElement("creator");
$song->appendChild($song1);
$text = $dom->createTextNode("www.musicking.in");
$song1->appendChild($text); 
$song1 = $dom->createElement("title");
$song->appendChild($song1);
// create text node
$text = $dom->createTextNode($tokenized);
$song1->appendChild($text); 
$tokenized = strtok($tokens);
$song1 = $dom->createElement("location");
$song->appendChild($song1);
$text = $dom->createTextNode($tokenized);
$song1->appendChild($text); 

}
DOMDocument::saveXML();

$con = mysql_connect("localhost","music123_sri","password");
 if (!$con) 
  { die('Could not connect: ' . mysql_error()); } 
mysql_select_db("music123_telugu", $con);
 $sql="INSERT INTO xml (File) VALUES (" . mysql_escape_string($xmlString) . ")"; 
 $data = mysql_query("SELECT File FROM xml")

 $info = mysql_fetch_array( $data );
?>
<object data="musicplayer.swf?autostart=true&playlist=<?php $info ?>"      type="application/x-shockwave-flash" width="400" height="300"><param name="movie"   value="musicplayer.swf?autostart=true&playlist=<?php $info ?>"/></object>



can u check the above code its not working.

答案 3 :(得分:-2)

我认为将它存储在数据库中是没有意义的,如果你使用单个文件问题已解决的另一个问题的解决方案(AGAIN !!),如果你将它存储在数据库中并从那里获取它或者这也可能有用,甚至可能是更好的解决方案,但是这不能用错误的值修复你的问题,因为这个php脚本将获得与另一个完全相同的值,它将导致完全相同的事情。

再次:

检查将数据发布到PHP脚本的代码!