2012-02-26 145 views
0
<?php 

require("phpsqlajax_dbinfo.php"); 
$dom = new DOMDocument("1.0"); 
$dp = fopen('samp.xml', 'w'); 
$node = $dom->createElement("Groceries"); 
fwrite($dp, '$node'); 
$parnode = $dom->appendChild($node); 

$connection = mysql_connect($host, $user, $pass); 
if (!$connection) { 
    die('Not connected : ' . mysql_error()); 
} 

$db_selected = mysql_select_db($database, $connection); 
if (!$db_selected) { 
    die ('Can\'t use db : ' . mysql_error()); 
} 

$query = "SELECT * FROM tbl_groceryitem"; 
$result = mysql_query($query); 
if (!$result) { 
    die('Invalid query: ' . mysql_error()); 
} 

echo "<groceries>"; 
while ($row = @mysql_fetch_assoc($result)) { 

    $node = $dom->createElement("item"); 
    echo "<echo>"; 
    fwrite($dp, '$node'); 
    $newnode = $parnode->appendChild($node); 
    $newnode->setAttribute("auto_id", $row['auto_id']); 
    echo "<auto_id>", $row[auto_id]; 
    fwrite($dp, '$newnode'); 
    $newnode->setAttribute("Gro_barcode", $row['Gro_barcode']); 
    echo "<Gro_barcode>", $row[Gro_barcode]; 
    fwrite($dp, '$newnode'); 
    $newnode->setAttribute("Gro_name", $row['Gro_name']); 
    echo "<Gro_name>", $row[Gro_name]; 
    fwrite($dp, '$newnode'); 
    $newnode->setAttribute("Gro_brand", $row['Gro_brand']); 
    echo "<Gro_brand>", $row[Gro_brand]; 
    fwrite($dp, '$newnode'); 
    $newnode->setAttribute("Gro_category", $row['Gro_category']); 
    echo "<Gro_category>", $row[Gro_category]; 
    fwrite($dp, '$newnode'); 
    $newnode->setAttribute("Gro_aisle", $row['Gro_aisle']); 
    echo "<Gro_category>", $row[Gro_aisle]; 
    fwrite($dp, '$newnode'); 
    $newnode->setAttribute("Gro_qty", $row['Gro_qty']); 
    echo "<Gro_qty>", $row[Gro_qty]; 
    fwrite($dp, '$newnode'); 
    $newnode->setAttribute("Gro_netwt", $row['Gro_netwt']); 
    echo "<Gro_netwt>", $row[Gro_netwt]; 
    fwrite($dp, '$newnode'); 
    $newnode->setAttribute("Gro_pic", $row['Gro_pic']); 
    echo "<Gro_pic>", $row[Gro_pic]; 
    fwrite($dp, '$newnode'); 
    $newnode->setAttribute("Gro_price", $row['Gro_price']); 
    echo "<Gro_price>", $row[Gro_price]; 
    fwrite($dp, '$newnode'); 
    $newnode->setAttribute("Gro_tax", $row['Gro_tax']); 
    echo "<Gro_tax>", $row[Gro_tax]; 
    fwrite($dp, '$newnode'); 
    echo "</item>"; 
    fwrite($dp, '</item>'); 
} 
fwrite($dp, '</groceries'); 
echo $dom->saveXML($xml); 

?> 

我是新來的PHP。Php生成XML

我創建了一個可以用myPHPAdmin中的數據生成xml文件的php文件。感謝:D。希望可以有人幫幫我。

首先嚐試,代碼已經顯示在php中,當我打開檢查創建的XML。顯示'$node',它反映了fwrite內部的確切字符串,當我嘗試刪除字符串quote(''),像這樣fwrite($dp,$node);。我得到錯誤。

而當我嘗試將代碼返回給fwrite($ dp,'$ node');.沒有顯示。 XML頁面是空白的。

+3

OMG,首先讓我歡迎你來到社區。請**注意**您不需要上傳您的所有代碼就問題。請僅張貼與該問題相關的代碼。否則,我們不會對回答它們感興趣。 – Starx 2012-02-26 08:45:12

+0

phpMyAdmin不允許你直接下載XML格式的數據嗎? – liquorvicar 2012-02-26 08:48:16

+0

爲什麼使用DOM來創建一個新的XML文件並打開一個XML文件來編寫。首先檢查我的答案是否爲寫入方式 – Starx 2012-02-26 08:54:17

回答

0

當您使用PHP創建XML文件時,您需要使用XML標頭。

<?xml version="1.0" encoding="utf-8"?> 

但是,當您在最後使用saveXML()時不需要此功能。

這裏是一個例子,從php的手冊中挑選出來。

<?php 

$doc = new DOMDocument('1.0'); 
// we want a nice output 
$doc->formatOutput = true; 

$root = $doc->createElement('book'); 
$root = $doc->appendChild($root); 

$title = $doc->createElement('title'); 
$title = $root->appendChild($title); 

$text = $doc->createTextNode('This is the title'); 
$text = $title->appendChild($text); 

echo "Saving all the document:\n"; 
echo $doc->saveXML() . "\n"; 

echo "Saving only the title part:\n"; 
echo $doc->saveXML($title); 
?>