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 相关文章推荐
php5 and xml示例
Nov 22 PHP
php 获得汉字拼音首字母的函数
Aug 01 PHP
php程序效率优化的一些策略小结
Jul 17 PHP
基于PHP Web开发MVC框架的Smarty使用说明
Apr 19 PHP
php实现telnet功能示例
Apr 08 PHP
php实现获取文章内容第一张图片的方法
Nov 04 PHP
PHP实现QQ快速登录的方法
Sep 28 PHP
php curl简单采集图片生成base64编码(并附curl函数参数说明)
Feb 15 PHP
laravel实现查询最后执行的一条sql语句的方法
Oct 09 PHP
PhpStorm 如何优雅的调试Hyperf的方法步骤
Nov 24 PHP
PHP利用缓存处理用户注册时的邮箱验证,成功后用户数据存入数据库操作示例
Dec 31 PHP
php输出形式实例整理
May 05 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
本地机apache配置基于域名的虚拟主机详解
2013/08/10 PHP
php单例模式实现方法分析
2015/03/14 PHP
php生成固定长度纯数字编码的方法
2015/07/09 PHP
通过Email发送PHP错误的方法
2015/07/20 PHP
PHP Static延迟静态绑定用法分析
2016/03/16 PHP
PHP手机号中间四位用星号*代替显示的实例
2017/06/02 PHP
PHP设计模式之装饰器模式实例详解
2018/02/07 PHP
Laravel框架路由和控制器的绑定操作方法
2018/06/12 PHP
tp5实现微信小程序多图片上传到服务器功能
2018/07/16 PHP
javascript复制对象使用说明
2011/06/28 Javascript
基于jquery的bankInput银行卡账号格式化
2012/08/22 Javascript
如何获取网站icon有哪些可行的方法
2014/06/05 Javascript
jQuery事件绑定用法详解(附bind和live的区别)
2016/01/19 Javascript
详解windows下vue-cli及webpack 构建网站(三)使用组件
2017/06/17 Javascript
nodejs结合Socket.IO实现的即时通讯功能详解
2018/01/12 NodeJs
微信小程序点击列表跳转到对应详情页过程解析
2019/09/26 Javascript
python制作爬虫并将抓取结果保存到excel中
2016/04/06 Python
python通过elixir包操作mysql数据库实例代码
2018/01/31 Python
Python操作redis实例小结【String、Hash、List、Set等】
2019/05/16 Python
python-序列解包(对可迭代元素的快速取值方法)
2019/08/24 Python
PyCharm搭建Spark开发环境的实现步骤
2019/09/05 Python
python实现的汉诺塔算法示例
2019/10/23 Python
python实现xml转json文件的示例代码
2020/12/30 Python
介绍CSS3使用技巧5个
2009/04/02 HTML / CSS
毕业自我鉴定范文
2013/11/06 职场文书
工程监理应届生求职信
2013/11/09 职场文书
出生公证书样本
2014/04/04 职场文书
研发工程师岗位职责
2014/04/28 职场文书
中学生社会实践活动总结
2014/07/03 职场文书
教师党的群众路线学习心得体会
2014/11/04 职场文书
实习工作表现评语
2014/12/31 职场文书
小学中队活动总结
2015/05/11 职场文书
2015年秋季开学典礼校长致辞
2015/07/16 职场文书
Nginx服务器添加Systemd自定义服务过程解析
2021/03/31 Servers
python 如何用terminal输入参数
2021/05/25 Python
python模块与C和C++动态库相互调用实现过程示例
2021/11/02 Python