<?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文件,可以使用myPHPAdmin中的数据生成xml文件。感谢:D。希望可以有人帮帮我。
首先尝试,代码已经在php中显示,当我打开以检查创建的XML时。显示为“$node
”,当我尝试删除字符串引号('')时,它反映了fwrite中的确切字符串。fwrite($dp,$node);
。我收到了错误。
当我尝试将代码返回到fwrite($ dp,'$ node');.没有显示。 XML页面是空白的。
答案 0 :(得分: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);
?>