本文實例講述了PHP實現(xiàn)瀏覽器格式化顯示XML的方法。分享給大家供大家參考,具體如下:
在頭部加上
header("Content-type: application/xml");
剛開始加上了發(fā)現(xiàn)還是不行。最近一直嘗試最后終于找到解決辦法。在代碼最后加上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;
![](http://img.jbzj.com/file_images/article/201901/201912294703386.png?201902294720)
終于顯示了,很爽
PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php面向對象程序設計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:- php使用simplexml_load_file加載XML文件并顯示XML的方法
- php的webservice的wsdl的XML無法顯示問題的解決方法
- PHP輸出XML到頁面的3種方法詳解
- php輸出xml必須header的解決方法
- php輸出xml格式字符串(用的這個)
- PHP輸出XML格式數(shù)據(jù)的方法總結
- php XMLWriter類的簡單示例代碼(RSS輸出)
- php輸出xml屬性的方法
- php獲取本地圖片文件并生成xml文件輸出具體思路
- PHP讀取并輸出XML文件數(shù)據(jù)的簡單實現(xiàn)方法