PHP实现的数组和XML文件相互转换功能示例

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP实现的数组和XML文件相互转换功能。分享给大家供大家参考,具体如下:

最近搞微信支付,微信服务器返回的都是XML文件,所以需要转换成数组,才会便于操作,好了话不多说,直接上代码:

1. XML转数组

/**
 * 将xml转为array
 * @param string  $xml xml字符串或者xml文件名
 * @param bool   $isfile 传入的是否是xml文件名
 * @return array  转换得到的数组
 */
function xmlToArray($xml,$isfile=false){
  //禁止引用外部xml实体
  libxml_disable_entity_loader(true);
  if($isfile){
    if(!file_exists($xml)) return false;
    $xmlstr = file_get_contents($xml);
  }else{
    $xmlstr = $xml;
  }
  $result= json_decode(json_encode(simplexml_load_string($xmlstr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  return $result;
}

用法示例:

$xmlDoc=<<<ETO
<books>
 <book>
 <author>Jack Herrington</author>
 <title>PHP Hacks</title>
 <publisher>O'Reilly</publisher>
 </book>
 <book>
 <author>Jack Herrington</author>
 <title>Podcasting Hacks</title>
 <publisher>O'Reilly</publisher>
 </book>
 <book>
 <author>XML格式化</author>
 <title>【宜配屋www.yipeiwu.com】在线工具</title>
 <publisher>tools.jb51.net</publisher>
 </book>
</books>
ETO;
$relarr=xmlToArray($xmlDoc);
print_r($relarr);

运行结果:

Array
(
  [book] => Array
    (
      [0] => Array
        (
          [author] => Jack Herrington
          [title] => PHP Hacks
          [publisher] => O'Reilly
        )
      [1] => Array
        (
          [author] => Jack Herrington
          [title] => Podcasting Hacks
          [publisher] => O'Reilly
        )
      [2] => Array
        (
          [author] => XML格式化
          [title] => 【宜配屋www.yipeiwu.com】在线工具
          [publisher] => tools.jb51.net
        )
    )
)

2. 数组转XML

/**
 * 数组转xml字符
 * @param string  $xml xml字符串
**/
function arrayToXml($data){
  if(!is_array($data) || count($data) <= 0){
    return false;
  }
  $xml = "<xml>";
  foreach ($data as $key=>$val){
    if (is_numeric($val)){
      $xml.="<".$key.">".$val."</".$key.">";
    }else{
      $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
    }
  }
  $xml.="</xml>";
  return $xml;
}

用法示例:

$arrDoc= array("author"=>"XML格式化","title"=>"【宜配屋www.yipeiwu.com】在线工具","publisher"=>"tools.jb51.net");
$xmlrel=arrayToXml($arrDoc);
//运行结果:<xml><author><![CDATA[XML格式化]]></author><title><![CDATA[【宜配屋www.yipeiwu.com】在线工具]]></title><publisher><![CDATA[tools.jb51.net]]></publisher></xml>

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基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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

相关文章

php中解析带中文字符的url函数分享

很多时候,在书写网页应用程序时候,会遇到中文和其他字符冲突的问题,例如有的url链接中包含中文字符,那么在使用wget/curl/file_get_contents等等获取信息时候会直接...

php strrpos()与strripos()函数

strripos() 函数 定义和用法strripos() 函数查找字符串在另一个字符串中最后一次出现的位置。如果成功,则返回位置,否则返回 false。 语法strrpos(strin...

浅谈php提交form表单

处理GET请求 实现的功能是输入姓名后页面显示“Hello XXX” 创建html文件hello.html: <!DOCTYPE html> <html>...

PHP eval函数使用介绍

代码: eval("echo'hello world';"); 上边代码等同于下边的代码: echo"hello world"; 在浏览器中都输出:hello world 运用ev...

PHP setcookie设置Cookie用法(及设置无效的问题)

结果碰到一个问题,setcookie设置了Cookie并没有生效,在浏览器端也没有看到。查了一下,原来是setcookie是通过HTTP请求响应的Header来完成的,需要在请求响应内容...