php xml 格式化,PHP实现浏览器格式化显示XML的方法示例
本文實例講述了PHP實現瀏覽器格式化顯示XML的方法。分享給大家供大家參考,具體如下:
在頭部加上
header("Content-type: application/xml");
剛開始加上了發現還是不行。最近一直嘗試最后終于找到解決辦法。在代碼最后加上exit;就可以了
$Dom = new \DOMDocument('1.0', 'utf-8');
$paper = $Dom->createElement('paper');
$Dom->appendChild($paper);
$exercises = $Dom->createElement('exercises');
$exercises->setAttribute('id','1');
$exercises->setAttribute('type','1');
$exercises->setAttribute('answer','1');
$paper->appendChild($exercises);
$title = $Dom->createElement('title');
$title->setAttribute('label','1');
$title->setAttribute('mapsrc','1');
$title->setAttribute('soundsrc','1');
$exercises->appendChild($title);
$option = $Dom->createElement('option');
$option->setAttribute('id','1');
$option->setAttribute('label','1');
$option->setAttribute('mapsrc','1');
$option->setAttribute('soundsrc','1');
$exercises->appendChild($option);
header("Content-type: application/xml");
echo $Dom->saveXml(); exit;
終于顯示了,很爽
PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:
希望本文所述對大家PHP程序設計有所幫助。
總結
以上是生活随笔為你收集整理的php xml 格式化,PHP实现浏览器格式化显示XML的方法示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用Java统计姓氏个数_JAVA统计数字
- 下一篇: php人民币转换,PHP字符串转换RMB