Xml.php 974 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * Xml class
  4. *
  5. * Xml处理
  6. *
  7. * @author gaoming13 <gaoming13@yeah.net>
  8. * @link https://github.com/gaoming13/wechat-php-sdk
  9. * @link http://me.diary8.com/
  10. */
  11. namespace echowx\utils;
  12. class Xml
  13. {
  14. /**
  15. * 生成xml字符串
  16. * @param array $params 数据数组
  17. * @return string
  18. */
  19. public static function toXml($params)
  20. {
  21. $xml = '<xml>';
  22. foreach ($params as $key => $val) {
  23. if (is_numeric($val)) {
  24. $xml .= '<'.$key.'>'.$val.'</'.$key.'>';
  25. } else {
  26. $xml .= '<'.$key.'><![CDATA['.$val.']]></'.$key.'>';
  27. }
  28. }
  29. $xml .= '</xml>';
  30. return $xml;
  31. }
  32. /**
  33. * xml转换成array
  34. * @param $xml 参数名称
  35. * return array 返回转换的array
  36. **/
  37. public static function toArr($xml) {
  38. $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  39. return $arr;
  40. }
  41. }