PHP生成嵌套式XML文件的问题

tulipelle 2009-11-08 05:50:39

有如上一个数据库,现在就是想用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>


想了很久没有想出结果出来。希望高手们指点一下
...全文
239 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tulipelle 2009-11-16
  • 打赏
  • 举报
回复
是不是要用到 htmlspecialchars()这个函数的?
阿_布 2009-11-15
  • 打赏
  • 举报
回复
在$sqlresults = mysql_query($sql);前面先执行
mysql_query("set names utf8");
tulipelle 2009-11-15
  • 打赏
  • 举报
回复
数据库是UTF-8的呀。文件是在DREAMWEAVER里写的。编码也是UTF-8的呀。搞不明白。唉
tulipelle 2009-11-15
  • 打赏
  • 举报
回复

谢谢楼上的。试过了,别的都没有乱码了。可是这个符号会有问题 '&' 。
看来还要写一句话在查询的时候 输出数据结果的时候 用 & 来代替 &
骄傲青蛙 2009-11-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tulipelle 的回复:]
我也写了一段代码生成了XML文件。可是却遇到了法文字符乱码的问题。我从数据读出来 用 echo 显示在浏览器中是没有乱码的。为什么生成XML文件后却存在很多字符乱码的问题呢?请教公鸡大哥

代码如下

PHP code<?phpheader("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>";?>
[/Quote]

将文件编码utf-8输出,
tony-杨 2009-11-13
  • 打赏
  • 举报
回复
乱码?是不是你程序或数据库编码不是utf8?
tulipelle 2009-11-13
  • 打赏
  • 举报
回复
我也写了一段代码生成了XML文件。可是却遇到了法文字符乱码的问题。我从数据读出来 用 echo 显示在浏览器中是没有乱码的。为什么生成XML文件后却存在很多字符乱码的问题呢?请教公鸡大哥

代码如下


<?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>";

?>
tulipelle 2009-11-13
  • 打赏
  • 举报
回复
自由火你不在啊?
程序猿之殇 2009-11-09
  • 打赏
  • 举报
回复
反正基本上就是这个样子,细节的地方你可以优化一下.


$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();
t240034137 2009-11-09
  • 打赏
  • 举报
回复
学习!向铁公鸡大哥学习!
tulipelle 2009-11-09
  • 打赏
  • 举报
回复
谢谢自由火。你的代码已经很优化了。呵呵。我会先试试的。
tulipelle 2009-11-08
  • 打赏
  • 举报
回复
没有人回答吗?在线等

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧