Discuz教程网

PHP中将数组转成XML格式的实现代码

[复制链接]
authicon dly 发表于 2011-9-3 19:00:37 | 显示全部楼层 |阅读模式
下面是网上的
代码如下:

  1. class ArrayToXML
  2. {
  3. /**
  4. * The main function for converting to an XML document.
  5. * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
  6. *
  7. * @param array $data
  8. * @param string $rootNodeName - what you want the root node to be - defaultsto data.
  9. * @param SimpleXMLElement $xml - should only be used recursively
  10. * @return string XML
  11. */
  12. public static function toXml($data, $rootNodeName = 'data', $xml=null)
  13. {
  14. // turn off compatibility mode as simple xml throws a wobbly if you don't.
  15. if (ini_get('zend.ze1_compatibility_mode') == 1)
  16. {
  17. ini_set ('zend.ze1_compatibility_mode', 0);
  18. }
  19. if ($xml == null)
  20. {
  21. $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
  22. }
  23. // loop through the data passed in.
  24. foreach($data as $key => $value)
  25. {
  26. // no numeric keys in our xml please!
  27. if (is_numeric($key))
  28. {
  29. // make string key...
  30. $key = "unknownNode_". (string) $key;
  31. }
  32. // replace anything not alpha numeric
  33. $key = preg_replace('/[^a-z]/i', '', $key);
  34. // if there is another array found recrusively call this function
  35. if (is_array($value))
  36. {
  37. $node = $xml->addChild($key);
  38. // recrusive call.
  39. ArrayToXML::toXml($value, $rootNodeName, $node);
  40. }
  41. else
  42. {
  43. // add single node.
  44. $value = htmlentities($value);
  45. $xml->addChild($key,$value);
  46. }
  47. }
  48. // pass back as string. or simple xml object if you want!
  49. return $xml->asXML();
  50. }
  51. }
复制代码




下面是我编辑过的代码
代码如下:

  1. function arrtoxml($arr,$dom=0,$item=0){
  2. if (!$dom){
  3. $dom = new DOMDocument("1.0");
  4. }
  5. if(!$item){
  6. $item = $dom->createElement("root");
  7. $dom->appendChild($item);
  8. }
  9. foreach ($arr as $key=>$val){
  10. $itemx = $dom->createElement(is_string($key)?$key:"item");
  11. $item->appendChild($itemx);
  12. if (!is_array($val)){
  13. $text = $dom->createTextNode($val);
  14. $itemx->appendChild($text);
  15. }else {
  16. arrtoxml($val,$dom,$itemx);
  17. }
  18. }
  19. return $dom->saveXML();
  20. }
复制代码



数组转换成XML格式
代码如下:

  1. <?
  2. $elementLevel = 0 ;
  3. function array_Xml($array, $keys = '')
  4. {
  5. global $elementLevel;
  6. if(!is_array($array))
  7. {
  8. if($keys == ''){
  9. return $array;
  10. }else{
  11. return "\n<$keys>" . $array . "</$keys>";
  12. }
  13. }else{
  14. foreach ($array as $key => $value)
  15. {
  16. $haveTag = true;
  17. if (is_numeric($key))
  18. {
  19. $key = $keys;
  20. $haveTag = false;
  21. }
  22. /**
  23. * The first element
  24. */
  25. if($elementLevel == 0 )
  26. {
  27. $startElement = "<$key>";
  28. $endElement = "</$key>";
  29. }
  30. $text .= $startElement."\n";
  31. /**
  32. * Other elements
  33. */
  34. if(!$haveTag)
  35. {
  36. $elementLevel++;
  37. $text .= "<$key>" . array_Xml($value, $key). "</$key>\n";
  38. }else{
  39. $elementLevel++;
  40. $text .= array_Xml($value, $key);
  41. }
  42. $text .= $endElement."\n";
  43. }
  44. }
  45. return $text;
  46. }
  47. ?>
复制代码



函数描述及例子
代码如下:

  1. <?
  2. $array = array(
  3. "employees" => array(
  4. "employee" => array(
  5. array(
  6. "name" => "name one",
  7. "position" => "position one"
  8. ),
  9. array(
  10. "name" => "name two",
  11. "position" => "position two"
  12. ),
  13. array(
  14. "name" => "name three",
  15. "position" => "position three"
  16. )
  17. )
  18. )
  19. );
  20. echo array_Xml($array);
  21. ?>
复制代码







上一篇:PHP 防注入函数(格式化数据)
下一篇:PHP记录日志的实现代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 20:34

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表