php实现的xml操作类


Posted in PHP onJanuary 15, 2016

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

<?php
/*
使用方法:
$test=new xml();
$test->new_xml('test.xml');
$test->root('document');
$test->append_root_node('book');
$test->append_child_node('author','linage');
$test->append_child_node('page',100);
$test->append_child_node('money','35 RMB');
$test->append_root_node_end();
$test->append_root_node('book','name','The"Web"Servers');
$test->append_child_node('a u t ho"r','li n a g e');
$test->append_child_node('page',100);
$test->append_child_node('money','35 RMB');
$test->append_root_node_end();
$test->display();
$test->save();
生成的xml结果:
<?xml version="1.0" encoding="utf-8"?>
<document>
<book>
<author>linage</author>
<page>100</page>
<money>35 RMB</money>
</book>
<book name="TheWebServers">
<author>li n a g e</author>
<page>100</page>
<money>35 RMB</money>
</book>
</document>
*/
class xml{
var $version;
var $encoding;
var $start;
var $end;
var $filename;
var $xml_document;
var $root_start;
var $root_end;
var $rss_start;
var $rss_end;
function xml($ver='1.0',$encoding='GB2312'){
 $this->version="<?xml version=/"{$ver}/" encoding=/"{$encoding}/" standalone=/"yes/" ?>";
 $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]/">";
 $this->rss_end="</rss>";
}
function new_xml($filename){
 $this->filename=$filename;
 return true;
}
function root($element){
 $element=$this->filter($element);
 if(isset($this->start) and isset($this->end)){
 exit("error:Only one top level element is allowed in an XML document./r/n");
 }else{
 $this->start="<$element>";
 $this->end="</$element>";
 $this->xml_document=$this->version."/n".$this->rss_start."/n".$this->start."/n";
 return true;
 }
}
function append_root_node($title,$property=null,$pro_val=null){
 $title=$this->filter($title);
 $property=$this->filter($property);
 $pro_val=$this->filter($pro_val);
 $property!=null?$pro_str=" $property=/"$pro_val/"":$property=null;
 $contents="<{$title}{$pro_str}>/n";
 $this->xml_document.=$contents;
 $this->root_end="</$title>";
 return true;
}
function append_root_node_end(){
 $this->xml_document.=$this->root_end."/n";
 return true;
}
function append_child_node($title='undefined',$contents='undefined',$property=null,$pro_val=null,$cddate=false){
 isset($property)?$pro_str=" $property=/"$pro_val/"":$property=null;
 $title=$this->filter($title);
 $contents=$this->filter($contents,false);
 $property=$this->filter($property);
 $pro_val=$this->filter($pro_val);
 $cddate===false?$cddate=false:$cddate=true;
 if($cddate){
 $contents="<{$title}{$pro_str}><!--[CDATA['/n$contents/n']]--></$title>/n";
 }else{
 $contents="<{$title}{$pro_str}>$contents</$title>";
 }
 $this->xml_document.=$contents."/n";
 return true;
}
function display(){
 header("Content-type: text/xml");
 $xml=$this->xml_document.$this->end."/n".$this->rss_end;
 echo $xml;
 //return true;
}
function filter($sring,$replace_null=true){
 $filter[]='"';
 $filter[]="//";
 $filter[]="/n";
 $filter[]="/r";
 $filter[]="/t";
 $replace_null===true?$filter[]=" ":$replace_null=false;
 foreach ($filter as $val){
 $sring=str_replace($val,'',$sring);
 }
 return $sring;
}
function encode(){
 //you can add the convert encode function here or add other class to do that
}
function save(){
 $this->xml_document=$this->xml_document.$this->end."/n".$this->rss_end;
 $handle=fopen($this->filename,'wb+');
 $result=fwrite($handle,$this->xml_document);
 fclose($handle);
 if($result){
 return true;
 }else{
 echo "error:can't write to files,maybe the access denied.try to chmod 777 the directory?";
 return false;
 }
}
}

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

PHP 相关文章推荐
第十节 抽象方法和抽象类 [10]
Oct 09 PHP
php+ajax做仿百度搜索下拉自动提示框(有实例)
Aug 21 PHP
win7+apache+php+mysql环境配置操作详解
Jun 10 PHP
php正则匹配html中带class的div并选取其中内容的方法
Jan 13 PHP
php实现用于删除整个目录的递归函数
Mar 16 PHP
PHP输出缓冲控制Output Control系列函数详解
Jul 02 PHP
实现PHP框架系列文章(6)mysql数据库方法
Mar 04 PHP
php构造函数与析构函数
Apr 23 PHP
PHP实现小偷程序实例
Oct 31 PHP
Yii2框架制作RESTful风格的API快速入门教程
Nov 08 PHP
Thinkphp 3.2框架使用Redis的方法详解
Oct 24 PHP
如何在Mac上通过docker配置PHP开发环境
May 29 PHP
PHP基于单例模式实现的数据库操作基类
Jan 15 #PHP
Linux安装配置php环境的方法
Jan 14 #PHP
PHP实现QQ登录实例代码
Jan 14 #PHP
PHP实现图片不变型裁剪及图片按比例裁剪的方法
Jan 14 #PHP
详解HTTP Cookie状态管理机制
Jan 14 #PHP
在php中设置session用memcache来存储的方法总结
Jan 14 #PHP
thinkphp实现图片上传功能
Jan 13 #PHP
You might like
详解PHP中的状态模式编程
2015/08/11 PHP
关于火狐(firefox)及ie下event获取的两种方法
2012/12/27 Javascript
JS异常处理的一个想法(sofish)
2013/03/14 Javascript
javascript截取字符串(通过substring实现并支持中英文混合)
2013/06/24 Javascript
jQuery实现两款有动画功能的导航菜单代码
2015/09/16 Javascript
jquery实现select选择框内容左右移动代码分享
2015/11/21 Javascript
AngularJS 避繁就简的路由
2016/07/01 Javascript
JavaScript里 ==与===区别详解
2016/08/16 Javascript
Angular2使用vscode断点调试ts文件的方法
2017/12/13 Javascript
JavaScript动态加载重复绑定问题
2018/04/01 Javascript
nodejs 递归拷贝、读取目录下所有文件和目录
2019/07/18 NodeJs
Net微信网页开发 使用微信JS-SDK获取当前地理位置过程详解
2019/08/26 Javascript
如何基于javascript实现贪吃蛇游戏
2020/02/09 Javascript
Vue js with语句原理及用法解析
2020/09/03 Javascript
[03:12]完美世界DOTA2联赛PWL DAY6集锦
2020/11/05 DOTA
用Python展示动态规则法用以解决重叠子问题的示例
2015/04/02 Python
Python中的匿名函数使用简介
2015/04/27 Python
Python学习小技巧之列表项的推导式与过滤操作
2017/05/20 Python
python输入错误密码用户锁定实现方法
2017/11/27 Python
利用python实现微信头像加红色数字功能
2018/03/26 Python
Python实现的线性回归算法示例【附csv文件下载】
2018/12/29 Python
在python 不同时区之间的差值与转换方法
2019/01/14 Python
python实现维吉尼亚算法
2019/03/20 Python
Python代码太长换行的实现
2019/07/05 Python
Pytorch高阶OP操作where,gather原理
2020/04/30 Python
解决Keras的自定义lambda层去reshape张量时model保存出错问题
2020/07/01 Python
Django Form常用功能及代码示例
2020/10/13 Python
美国排名第一的葡萄酒俱乐部:Firstleaf Wine Club
2020/01/02 全球购物
保险专业大专生求职信
2013/10/26 职场文书
水利公司纪检监察自我鉴定
2014/02/25 职场文书
2014年冬季防火方案
2014/05/21 职场文书
奉献爱心演讲稿
2014/09/04 职场文书
无犯罪记录证明
2014/09/19 职场文书
加强作风建设演讲稿
2014/10/24 职场文书
2015年化验室工作总结
2015/04/23 职场文书
mybatis调用sqlserver存储过程返回结果集的方法
2021/05/08 SQL Server