PHP实现浏览器格式化显示XML的方法示例

yipeiwu_com6年前PHP代码库

本文实例讲述了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操作的在线工具供大家参考使用:

在线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数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php相当简单的分页类

class Helper_Page{ /** 总信息数 */ var $infoCount; /** 总页数 */ var $pageCount; /** 每页显示条数 */ var $...

PHP设计模式之装饰者模式

PHP设计模式之装饰者模式

介绍 装饰者模式动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。 思维导图   有这样一个项目,做一个餐厅订餐系统。起初的代码结构是这样的。前...

linux使用crontab实现PHP执行计划定时任务

linux使用crontab实现PHP执行计划定时任务

首先说说cron,它是一个linux下的定时执行工具。根用户以外的用户可以使用 crontab 工具来配置 cron 任务。所有用户定义的 crontab 都被保存在/var/spool...

PHP魔术方法之__call与__callStatic使用方法

核心代码 //魔术方法__call /* $method 获得方法名 $arg 获得方法的参数集合 */ class Human { private function t(...

php 常用类整理

图表库 下面的类库可以让你很简单就能创建复杂的图表和图片。当然,它们需要GD库的支持。 pChart - 一个可以创建统计图的库。 Libchart - 这也是一个简单的统计图库。 Jp...