利用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;

利用PHP怎么格式化显示XML

以上就是利用PHP怎么格式化显示XML,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。