有如上一个数据库,现在就是想用PHP读取后生成一个XML文件。但是这个XML文件是嵌套式的 如下格式:
<note label = "root">
<note label = "01 - Management Opérationnel">
<note label = "Fondamentaux du management">
<note label ="Animer une équipe en production" />
<note label ="Manager en transverse " />
<note label ="Passport to People Management" />
</note>
<note label = "Perfectionner son management">
<note label ="Donner et recevoir du Feedback"/>
<note label ="Team management"/>
</note label>
</note>
<note label = "02 - Marketing & Ventes, Stratégie">
.
.
. </note>
</note>
想了很久没有想出结果出来。希望高手们指点一下
$conn = mysql_connect('localhost','xxx','xxx');
mysql_select_db('xxx');
$sql = "SELECT distinct(domaine) FROM test4 ORDER BY domaine ASC";
$result = mysql_query($sql);
$dom = new DOMDocument('1.0', 'utf-8');$note = new DOMElement('note');
$e1 = $dom->appendChild($note);
$e1->setAttribute("label", "root");$i = 0;
while($row = mysql_fetch_object($result))
{
$i++;
$note = new DOMElement('note');
$e2 = $e1->appendChild($note);
$e2->setAttribute("label", sprintf("%02d-%s", $i, $row->domaine));
$sql = "SELECT distinct(theme) FROM test4 WHERE domaine = '" .$row->domaine. "' ORDER BY theme ASC ";
$res2 = mysql_query($sql);
while($row2 = mysql_fetch_object($res2))
{
$note = new DOMElement('note');
$e3 = $e2->appendChild($note);
$e3->setAttribute("label", $row2->theme);
$sql = "SELECT distinct(libellestage) FROM test4 WHERE theme = '" .$row2->theme. "' ORDER BY libellestage ASC ";
$res3 = mysql_query($sql);
while($row3 = mysql_fetch_object($res3))
{
$note = new DOMElement('note');
$e4 = $e3->appendChild($note);
$e4->setAttribute("label", $row3->libellestage);
}
}
}
header("content-type: application/xml");
echo $dom->saveXML();
<?php
header("Content-type: application/xml");
include "ini.php";
dbconnect();
$sql = "SELECT Domaine, Theme, LibelleStage, CodeTheme
FROM catalogue ";
$sqlresults = mysql_query($sql);//premiere niveau de XML root
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n<note label =\"root\">\r\n";
$tempC ==NULL;
$tempD ==NULL;
for($x = 0 ; $x < mysql_num_rows($sqlresults) ; $x++){
$row = mysql_fetch_assoc($sqlresults);
if(is_null($tempC) && is_null($tempD)){
$_xml .= "\t<note label = \" ".$row['Domaine']." \">\r\n";
$_xml .= "\t<note label = \" ".$row['CodeTheme']." \">\r\n";
$_xml .= "\t<note label = \" ".$row['LibelleStage']." \"/>\r\n";
}else{
if ($tempD != $row['Domaine']){
$_xml .= "</note>\r\n";
$_xml .= "</note>\r\n";
$_xml .= "\t<note label = \" ".$row['Domaine']." \">\r\n";
$_xml .= "\t<note label = \" ".$row['CodeTheme']." \">\r\n";
$_xml .= "\t<note label = \" ".$row['LibelleStage']." \"/>\r\n";
}
else { if ($tempC !=$row['CodeTheme']){
$_xml .= "</note>\r\n";
$_xml .= "\t<note label = \" ".$row['CodeTheme']." \">\r\n";
$_xml .= "\t<note label = \" ".$row['LibelleStage']." \"/>\r\n";
} else{
$_xml .= "\t<note label = \" ".$row['LibelleStage']." \"/>\r\n";
} } } $tempC = $row['CodeTheme'];
$tempD = $row['Domaine'];
}
$_xml .= "</note>\r\n";
$_xml .= "</note>\r\n";
$_xml .= "</note>";$file= fopen("cours.xml", "w");
fwrite($file, $_xml);
fclose($file);echo "XML has been written. <a href=\"cours.xml\">View the XML.</a>";
?>
将文件编码utf-8输出,
mysql_query("set names utf8");
谢谢楼上的。试过了,别的都没有乱码了。可是这个符号会有问题 '&' 。
看来还要写一句话在查询的时候 输出数据结果的时候 用 & 来代替 &