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 相关文章推荐
PHP通用分页类page.php[仿google分页]
Aug 31 PHP
PHP 字符截取 解决中文的截取问题,不用mb系列
Sep 29 PHP
PHP 递归效率分析
Nov 24 PHP
dedecms系统的广告设置代码 基础版本
Apr 09 PHP
PHP的MVC模式实现原理分析(一相简单的MVC框架范例)
Apr 29 PHP
php实现curl模拟ftp上传的方法
Jul 29 PHP
win7安装php框架Yii的方法
Jan 25 PHP
100行PHP代码实现socks5代理服务器
Apr 28 PHP
PHP实现冒泡排序的简单实例
May 26 PHP
PHP中的多种加密技术及代码示例解析
Oct 20 PHP
PHP实现类似题库抽题效果
Aug 16 PHP
详解PHP Swoole与TCP三次握手
May 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
简单采集了yahoo的一些数据
2007/02/14 PHP
PHP 截取字符串 分别适合GB2312和UTF8编码情况
2009/02/12 PHP
PHP+MySQL投票系统的设计和实现分享
2012/09/23 PHP
比较strtr, str_replace和preg_replace三个函数的效率
2013/06/26 PHP
PHP+shell脚本操作Memcached和Apache Status的实例分享
2016/03/11 PHP
关于恒等于(===)和非恒等于(!==)
2007/08/20 Javascript
javascript 类定义的4种方法
2009/09/12 Javascript
让Firefox支持event对象实现代码
2009/11/07 Javascript
表单验证的完整应用案例探讨
2013/03/29 Javascript
JS中window.open全屏命令解析及使用示例
2013/12/11 Javascript
jQuery使用empty()方法删除元素及其所有子元素的方法
2015/03/26 Javascript
bootstrap监听滚动实现头部跟随滚动
2016/11/08 Javascript
老生常谈jquery中detach()和remove()的区别
2017/03/02 Javascript
Vue开发中整合axios的文件整理
2017/04/29 Javascript
JavaScript 中调用 Kotlin 方法实例详解
2017/06/09 Javascript
Angular开发实践之服务端渲染
2018/03/29 Javascript
JS阻止事件冒泡的方法详解
2019/08/26 Javascript
NProgress显示顶部进度条效果及使用详解
2019/09/21 Javascript
原生javascript如何实现共享onload事件
2020/07/03 Javascript
Postman如何实现参数化执行及断言处理
2020/07/28 Javascript
javascript实现京东快递单号的查询效果
2020/11/30 Javascript
python hough变换检测直线的实现方法
2019/07/12 Python
python之拟合的实现
2019/07/19 Python
python实现企业微信定时发送文本消息的实例代码
2020/11/25 Python
印尼最大的网上书店:Gramedia.com
2018/09/13 全球购物
Theo + George官方网站:都柏林时尚品牌
2019/04/08 全球购物
汽车销售求职自荐信
2013/10/01 职场文书
写给保洁员表扬信
2014/01/08 职场文书
学校卫生检查制度
2014/02/03 职场文书
大学生学习2014全国两会心得体会
2014/03/13 职场文书
小学生思想品德评语
2014/12/31 职场文书
幼师小班个人总结
2015/02/12 职场文书
推广普通话的宣传语
2015/07/13 职场文书
曾国藩励志经典名言37句,蕴含哲理
2019/10/14 职场文书
漫画「古见同学有交流障碍症」第25卷封面公开
2022/03/21 日漫
 python中的元类metaclass详情
2022/05/30 Python