当前位置:首页 > PHP教程 > php类库 > 列表

php实现的xml操作类

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-06 09:52:49 浏览: 评论:0 

这篇文章主要介绍了php实现的xml操作类,涉及PHP针对xml文件的创建、读取、节点操作等常用技巧,需要的朋友可以参考下。

本文实例讲述了php实现的xml操作类,分享给大家供大家参考,具体如下:

  1. <?php 
  2. /* 
  3. 使用方法: 
  4. $test=new xml(); 
  5. $test->new_xml('test.xml'); 
  6. $test->root('document'); 
  7. $test->append_root_node('book'); 
  8. $test->append_child_node('author','linage'); 
  9. $test->append_child_node('page',100); 
  10. $test->append_child_node('money','35 RMB'); 
  11. $test->append_root_node_end(); 
  12. $test->append_root_node('book','name','The"Web"Servers'); 
  13. $test->append_child_node('a u t ho"r','li n a g e'); 
  14. $test->append_child_node('page',100); 
  15. $test->append_child_node('money','35 RMB'); 
  16. $test->append_root_node_end(); 
  17. $test->display(); 
  18. $test->save(); 
  19. 生成的xml结果: 
  20. <?xml version="1.0" encoding="utf-8"?> 
  21. <document> 
  22. <book> 
  23. <author>linage</author> 
  24. <page>100</page> 
  25. <money>35 RMB</money> 
  26. </book> 
  27. <book name="TheWebServers"> 
  28. <author>li n a g e</author> 
  29. <page>100</page> 
  30. <money>35 RMB</money> 
  31. </book> 
  32. </document> 
  33. */ 
  34. class xml{ 
  35. var $version; 
  36. var $encoding; 
  37. var $start; 
  38. var $end; 
  39. var $filename; 
  40. var $xml_document; 
  41. var $root_start; 
  42. var $root_end; 
  43. var $rss_start; 
  44. var $rss_end; 
  45. function xml($ver='1.0',$encoding='GB2312'){ 
  46.  $this->version="<?xml version=/"{$ver}/" encoding=/"{$encoding}/" standalone=/"yes/" ?>"; 
  47.  $this->rss_start="<rss version=/"2.0/" xmlns:domxml=/"[url]http://xml.666life.com/rss/1.0[/url]/" xmlns:geo=/"[url]http://www.w3.org/2003/01/geo/wgs84_pos#[/url]/">"; 
  48.  $this->rss_end="</rss>"; 
  49. } 
  50. function new_xml($filename){ 
  51.  $this->filename=$filename; 
  52.  return true; 
  53. } 
  54. function root($element){ 
  55.  $element=$this->filter($element); 
  56.  if(isset($this->start) and isset($this->end)){ 
  57.  exit("error:Only one top level element is allowed in an XML document./r/n"); 
  58.  }else{ 
  59.  $this->start="<$element>"; 
  60.  $this->end="</$element>"; 
  61.  $this->xml_document=$this->version."/n".$this->rss_start."/n".$this->start."/n"; 
  62.  return true; 
  63.  } 
  64. } 
  65. function append_root_node($title,$property=null,$pro_val=null){ 
  66.  $title=$this->filter($title); 
  67.  $property=$this->filter($property); 
  68.  $pro_val=$this->filter($pro_val); 
  69.  $property!=null?$pro_str=" $property=/"$pro_val/"":$property=null; 
  70.  $contents="<{$title}{$pro_str}>/n"; 
  71.  $this->xml_document.=$contents; 
  72.  $this->root_end="</$title>"; 
  73.  return true; 
  74. } 
  75. function append_root_node_end(){ 
  76.  $this->xml_document.=$this->root_end."/n"; 
  77.  return true; 
  78. } 
  79. function append_child_node($title='undefined',$contents='undefined',$property=null,$pro_val=null,$cddate=false){ 
  80.  isset($property)?$pro_str=" $property=/"$pro_val/"":$property=null; 
  81.  $title=$this->filter($title); 
  82.  $contents=$this->filter($contents,false); 
  83.  $property=$this->filter($property); 
  84.  $pro_val=$this->filter($pro_val); 
  85.  $cddate===false?$cddate=false:$cddate=true; 
  86.  if($cddate){ 
  87.  $contents="<{$title}{$pro_str}><!--[CDATA['/n$contents/n']]--></$title>/n"; 
  88.  }else{ 
  89.  $contents="<{$title}{$pro_str}>$contents</$title>"; 
  90.  } 
  91.  $this->xml_document.=$contents."/n"; 
  92.  return true; 
  93. } 
  94. function display(){ 
  95.  header("Content-type: text/xml"); 
  96.  $xml=$this->xml_document.$this->end."/n".$this->rss_end; 
  97.  echo $xml; 
  98.  //return true; 
  99. } 
  100. function filter($sring,$replace_null=true){ 
  101.  $filter[]='"'; 
  102.  $filter[]="//"; 
  103.  $filter[]="/n"; 
  104.  $filter[]="/r"; 
  105.  $filter[]="/t"; 
  106.  $replace_null===true?$filter[]=" ":$replace_null=false; 
  107.  foreach ($filter as $val){ 
  108.  $sring=str_replace($val,'',$sring); 
  109.  } 
  110.  return $sring; 
  111. } 
  112. function encode(){ 
  113.  //you can add the convert encode function here or add other class to do that 
  114. } 
  115. function save(){ 
  116.  $this->xml_document=$this->xml_document.$this->end."/n".$this->rss_end; 
  117.  $handle=fopen($this->filename,'wb+'); 
  118.  $result=fwrite($handle,$this->xml_document); 
  119.  fclose($handle); 
  120.  if($result){ 
  121.  return true; 
  122.  }else{ 
  123.  echo "error:can't write to files,maybe the access denied.try to chmod 777 the directory?"; 
  124.  return false; 
  125.  } 
  126. } 
  127. }

Tags: xml操作类

分享到: