1.PHP 生成 XML 数据

① 拼接字符串

② 使用系统类(DomDocument,XMLWriter,SimpleXML)

例1 使用 PHP 系统类中的 DomDocument 类:

<?php
$dom = new DomDocument('1.0','utf-8');
$element = $dom->createElement('test','This is a root element');
$dom->appendChild($element);
echo $dom->saveXML();

页面输出

This is a root element

查看源代码显示:

<?xml version="1.0" encoding="utf-8"?>
<test>This is a root element</test>

例2 拼接字符串

//修改 http 头信息
header("Content-Type:text/xml");
//xml头信息
$xml = "<?xml version='1.0' encoding='utf-8'?>\n";
//根节点开始标签
$xml .= "<root>\n";
//code
$xml .= "<code>200</code>\n";
//message
$xml .= "<message>数据返回成功</message>\n";
//data
$xml .= "<data>\n";
$xml .= "<id>1</id>\n";
$xml .= "<name>John</name>\n";
$xml .= "</data>\n";
//根节点结束标签
$xml .= "</root>"; echo $xml;
exit();

页面输出:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<root>
<code>200</code>
<message>数据返回成功</message>
<data>
<id>1</id>
<name>John</name>
</data>
</root>

http 响应头信息:

2.XML 方式封装通信接口

<?php

class Response{
/**
* 按 xml 方式输出通信数据
* @param integer $code 状态码
* @param string $message 提示信息
* @param array $data 数据
* return string
*/
public static function xml($code,$message,$data){ if(!is_numeric($code)){
return '';
} $result = array(
'code' => $code,
'message' => $message,
'data' => $data
); //修改 http 头信息
header("Content-Type:text/xml");
//xml头信息
$xml = "<?xml version='1.0' encoding='utf-8'?>";
//根节点开始标签
$xml .= "<root>"; $xml .= self::xmlToEncode($result); //根节点结束标签
$xml .= "</root>"; echo $xml;
exit();
} //解析$result至xml
public static function xmlToEncode($data){
$xml = $attr = "";
foreach($data as $k=>$v){
//如果$k是数字(data(code,message,data中的data)数据里面还含有索引数组),要进行如下判断
if(is_numeric($k)){
$attr = "id='{$k}'";
$k = 'item ';
} $xml .= "<{$k} {$attr}>";
//如果$v是数组,则递归调用该方法
if(is_array($v)){
$xml .= self::xmlToEncode($v);
}else{
$xml .= $v;
}
$xml .= "</{$k}>";
} return $xml;
}
}

调用该页面 test.php

$data 第一种情况:

<?php
require 'response.php'; $data = array(
'id'=>1,
'name'=>'Mary'
);
Response::xml(200,'数据返回成功',$data);

页面输出:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<root>
<code>200</code>
<message>数据返回成功</message>
<data>
<id>1</id>
<name>Mary</name>
</data>
</root>

$data 第二种情况

<?php
require 'response.php'; $data = array(
'id'=>1,
'name'=>'Mary',
'type'=>array(1,3,6) //<0>1</0><1>3</1><2>6</2> => <item id="0">1</item>...
); Response::xml(200,'数据返回成功',$data);

页面输出:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<root>
<code>200</code>
<message>数据返回成功</message>
<data>
<id>1</id>
<name>Mary</name>
<type>
<item id="0">1</item>
<item id="1">3</item>
<item id="2">6</item>
</type>
</data>
</root>

$data 第三中情况:

<?php
require 'response.php'; $data = array(
'id'=>1,
'name'=>'Mary',
'type'=>array('a'=>1,'b'=>3,'c'=>6)
);
Response::xml(200,'数据返回成功',$data);

页面输出:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<root>
<code>200</code>
<message>数据返回成功</message>
<data>
<id>1</id>
<name>Mary</name>
<type>
<a>1</a>
<b>3</b>
<c>6</c>
</type>
</data>
</root>

最新文章

  1. Redis学习笔记九:独立功能之慢查询日志
  2. discuz后台开发常用函数
  3. App所需申请资料
  4. CentOS 6.6 中中文输入法设置
  5. 码农谷 找出N之内的所有完数
  6. [原创]Spring MVC 学习 之 - URL参数传递
  7. paip.关于动画特效原理 html js 框架总结
  8. JqueryEasyUI 解决IE下datagrid无法刷新的问题
  9. IOS学习之IOS沙盒(sandbox)机制和文件操作
  10. 实用工具推荐(Live Writer)(2015年05月26日)
  11. nginx+uwsgi+django
  12. (转)JavaWeb学习总结(十三)——使用Session防止表单重复提交
  13. App测试全(转自鲁德)
  14. 既然还看不到未来之光,那就从骄阳开始的地方--IT携行
  15. 如何在同一台电脑上使用两个github账户(亲测有效)
  16. 在cikuapi.com上抓取相关词
  17. Windows 10 更改系统文字大小
  18. Codeforces Hello 2019
  19. MD5、SHA1加密java 16位32位
  20. poj1845(逆元+快速幂)

热门文章

  1. 【读书笔记】读《编写高质量代码—Web前端开发修炼之道》 - JavaScript原型继承与面向对象
  2. redhad借用CentOs yum 安装
  3. wifi开发总结
  4. Twitter search API
  5. C++的那些事:容器和泛型算法
  6. 小甲鱼PE详解之输入表(导入表)详解2(PE详解08)
  7. 建模算法(六)&mdash;&mdash;神经网络模型
  8. Android中dp和px之间进行转换
  9. LoadRunner中多值关联的3种处理方式
  10. 计算几何 HDOJ 4720 Naive and Silly Muggles