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 相关文章推荐
Content-type 的说明
Oct 09 PHP
PHP音乐采集(部分代码)
Feb 14 PHP
PHP写入WRITE编码为UTF8的文件的实现代码
Jul 07 PHP
php 读取shell管道传输过来的内容
Mar 01 PHP
常见的PHP五种设计模式小结
Mar 23 PHP
非常精妙的PHP递归调用与静态变量使用
Dec 16 PHP
基于Linux调试工具strace与gdb的常用命令总结
Jun 03 PHP
php验证是否是md5编码的简单代码
Apr 01 PHP
PHP 面向对象程序设计(oop)学习笔记 (五) - PHP 命名空间
Jun 12 PHP
PHP实现微信提现功能
Sep 30 PHP
PDO::beginTransaction讲解
Jan 27 PHP
PHP的new static和new self的区别与使用
Nov 27 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 图片上传代码
2011/09/13 PHP
php 从指定数字中获取随机组合的简单方法(推荐)
2017/04/05 PHP
ThinkPHP整合datatables实现服务端分页的示例代码
2018/02/10 PHP
PHP基于DateTime类解决Unix时间戳与日期互转问题【针对1970年前及2038年后时间戳】
2018/06/13 PHP
javascript 进阶篇1 正则表达式,cookie管理,userData
2012/03/14 Javascript
Javascript call和apply区别及使用方法
2013/11/14 Javascript
使用jquery实现IE下按backspace相当于返回操作
2014/03/18 Javascript
2014最热门的JavaScript代码高亮插件推荐
2014/11/25 Javascript
Bootstrap每天必学之下拉菜单
2015/11/25 Javascript
js实现各种复制到剪贴板的方法(分享)
2016/10/27 Javascript
微信小程序 支付简单实例及注意事项
2017/01/06 Javascript
jquery对象与DOM对象转化
2017/02/08 Javascript
jQuery+PHP+Mysql实现抽奖程序
2020/04/12 jQuery
JS查找数组中重复元素的方法详解
2017/06/14 Javascript
vue.js简单配置axios的方法详解
2017/12/13 Javascript
layui表格checkbox选择全选样式及功能的实例
2018/03/07 Javascript
angularjs http与后台交互的实现示例
2018/12/21 Javascript
vue下axios拦截器token刷新机制的实例代码
2020/01/17 Javascript
vue路由传参的基本实现方式小结【三种方式】
2020/02/05 Javascript
记一次用ts+vuecli4重构项目的实现
2020/05/21 Javascript
在Django中创建第一个静态视图
2015/07/15 Python
python获取交互式ssh shell的方法
2019/02/14 Python
Django框架教程之中间件MiddleWare浅析
2019/12/29 Python
解决Pycharm的项目目录突然消失的问题
2020/01/20 Python
美国高级音响品牌:Master&Dynamic
2018/07/05 全球购物
美国球迷装备的第一来源:FOCO
2020/07/03 全球购物
牛津在线药房:Oxford Online Pharmacy
2020/11/16 全球购物
C++:局部变量能否和全局变量重名
2014/03/03 面试题
2013英文求职信模板范文
2013/11/15 职场文书
金属材料工程毕业生个人的自我评价
2013/11/28 职场文书
体育教师个人的自我评价
2014/02/16 职场文书
项目合作协议书
2014/09/23 职场文书
2015年办税服务厅工作总结
2015/07/23 职场文书
公司人力资源管理制度
2015/08/05 职场文书
mysql批量新增和存储的方法实例
2021/04/07 MySQL
Redis特殊数据类型HyperLogLog基数统计算法讲解
2022/06/01 Redis