首页 > 开发 > PHP > 正文

PHP输出生成XML文件实例程序

2024-05-04 21:58:37
字体:大 中 小
来源:转载
供稿:网友

在php中输出生成xml文件的方法有很多,有直接用header输入,也有使用DomDocument与SimpleXML实现创建xml文档的.

方法一,代码如下:

  1. function xml_out($content, $charset = 'utf-8') { 
  2.  @header("Expires: -1"); 
  3.  @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE); 
  4.  @header("Pragma: no-cache"); 
  5.  @header("Content-type: application/xml; charset=$charset"); 
  6.  echo '<' . "?xml version="1.0" encoding="$charset"?>n"; 
  7.  echo "<root><![CDATA[" . trim($content) . "]]></root>"; 
  8.  exit(); 
  9. } 

方法二,代码如下:

  1. <?php 
  2. header("Content-type: text/xml"); 
  3. echo "<?xml version="1.0" encoding="UTF-8"?>"; 
  4. echo "<users><user><name>小小菜鸟</name><age>24</age><sex>男</sex></user><user><name>艳艳</name><age>23</age><sex>女</sex></user></users>"; 
  5. ?> 

方法三:使用DomDocument生成XML文件

创建节点使用createElement方法,创建文本内容使用createTextNode方法,添加子节点使用appendChild方法,创建属性使用createAttribute方法,代码如下:

  1. <?PHP 
  2. $data_array = array( 
  3.     array( 
  4.     'title' => 'title1', 
  5.     'content' => 'content1', 
  6.         'pubdate' => '2009-10-11', 
  7.     ), 
  8.     array( 
  9.     'title' => 'title2', 
  10.     'content' => 'content2', 
  11.     'pubdate' => '2009-11-11', 
  12.     ) 
  13. ); 
  14.  
  15. //  属性数组 
  16. $attribute_array = array( 
  17.     'title' => array( 
  18.     'size' => 1 
  19.     ) 
  20. ); 
  21.  
  22. //  创建一个XML文档并设置XML版本和编码。。 
  23. $dom=new DomDocument('1.0', 'utf-8'); 
  24.  
  25. //  创建根节点 
  26. $article = $dom->createElement('article'); 
  27. $dom->appendchild($article); 
  28.  
  29. foreach ($data_array as $data) { 
  30.     $item = $dom->createElement('item'); 
  31.     $article->appendchild($item); 
  32.  
  33.     create_item($dom, $item, $data, $attribute_array); 
  34. } 
  35.  
  36. echo $dom->saveXML(); 
  37.  
  38. function create_item($dom, $item, $data, $attribute) { 
  39.     if (is_array($data)) { 
  40.         foreach ($data as $key => $val) { 
  41.             //  创建元素 
  42.             $$key = $dom->createElement($key); 
  43.             $item->appendchild($$key); 
  44.  
  45.             //  创建元素值 
  46.             $text = $dom->createTextNode($val); 
  47.             $$key->appendchild($text); 
  48.  
  49.             if (isset($attribute[$key])) {  //  如果此字段存在相关属性需要设置 
  50.                 foreach ($attribute[$key] as $akey => $row) { 
  51.                     //  创建属性节点 
  52.                     $$akey = $dom->createAttribute($akey); 
  53.                     $$key->appendchild($$akey); 
  54.  
  55.                     // 创建属性值节点 
  56.                     $aval = $dom->createTextNode($row); 
  57.                     $$akey->appendChild($aval); 
  58.                 }//开源代码Vevb.com 
  59.             }   //  end if 
  60.         } 
  61.     }   //  end if 
  62. }   //  end function 
  63. ?> 

方法四:SimpleXML输入xml格式编码

SimpleXML作为PHP核心的组成部分,可以把XML转换为对象,但是有时候,我需要对输出的xml格式设置编码,代码如下:

$XML = new SimpleXMLElement("<foo />"); echo($XML->asXML());
 
//输出结果:<?xml version="1.0"?> <foo/>
 
//如果想输出,代码如下:<?xml version="1.0" encoding="UTF-8"?> <foo/>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表