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 相关文章推荐
isset和empty的区别
Jan 15 PHP
php 破解防盗链图片函数
Dec 09 PHP
计算php页面运行时间的函数介绍
Jul 01 PHP
PHP错误和异长常处理总结
Mar 06 PHP
PHP生成RSS文件类实例
Dec 05 PHP
PHP模块memcached使用指南
Dec 08 PHP
Yii使用Captcha验证码的方法
Dec 28 PHP
phpinfo() 中 Local Value(局部变量)Master Value(主变量) 的区别
Feb 03 PHP
Yii开启片段缓存的方法
Mar 28 PHP
用php和jQuery来实现“顶”和“踩”的投票功能
Oct 13 PHP
基于CI框架的微信网页授权库示例
Nov 25 PHP
详解PHP多个进程配合redis的有序集合实现大文件去重
Mar 06 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
新闻分类录入、显示系统
2006/10/09 PHP
php自定义session示例分享
2014/04/22 PHP
深入理解PHP中的global
2014/08/19 PHP
ThinkPHP实现二级循环读取的方法
2014/11/03 PHP
php合并数组中相同元素的方法
2014/11/13 PHP
PHP单链表的实现代码
2016/07/05 PHP
PHP实现获取第一个中文首字母并进行排序的方法
2017/05/09 PHP
浅谈PHP面向对象之访问者模式+组合模式
2017/05/22 PHP
基于Jquery的仿照flash放大图片效果代码
2011/03/16 Javascript
node.js下when.js 的异步编程实践
2014/12/03 Javascript
JS动态添加Table的TR,TD实现方法
2015/01/28 Javascript
如何解决谷歌浏览器下jquery无法获取图片的尺寸
2015/09/10 Javascript
以WordPress为例讲解jQuery美化页面Title的方法
2016/05/23 Javascript
Google 地图API Map()构造器详解
2016/08/06 Javascript
JS+CSS3模拟溢出滚动效果
2016/08/12 Javascript
node.js中express中间件body-parser的介绍与用法详解
2017/05/23 Javascript
vue2.0 实现导航守卫(路由守卫)
2018/05/21 Javascript
layui select获取自定义属性方法
2018/08/15 Javascript
jquery操作select常见方法大全【7种情况】
2019/05/28 jQuery
解决layer.confirm选择完之后消息框不消失的问题
2019/09/16 Javascript
VUE+elementui面包屑实现动态路由详解
2019/11/04 Javascript
javascript实现时间日期的格式化的方法汇总
2020/08/06 Javascript
利用Python脚本在Nginx和uwsgi上部署MoinMoin的教程
2015/05/05 Python
远程部署工具Fabric详解(支持Python3)
2019/07/04 Python
Python 使用folium绘制leaflet地图的实现方法
2019/07/05 Python
python 同时读取多个文件的例子
2019/07/16 Python
Python类继承和多态原理解析
2020/02/05 Python
基于python实现生成指定大小txt文档
2020/07/20 Python
美国宠物商店:Wag.com
2016/10/25 全球购物
卫校中专生的自我评价
2014/01/15 职场文书
大学生工作求职信
2014/06/23 职场文书
2014小学年度工作总结
2014/12/20 职场文书
2015年人力资源部工作总结
2015/04/30 职场文书
Python卷积神经网络图片分类框架详解分析
2021/11/07 Python
Nginx设置HTTPS的方法步骤 443证书配置方法
2022/03/21 Servers
Nginx 匹配方式
2022/05/15 Servers