2012-02-26 237 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); 
?>