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控制网页过期时间的代码
Sep 28 PHP
PHP常用技巧总结(附函数代码)
Feb 04 PHP
php设计模式之单例、多例设计模式的应用分析
Jun 30 PHP
php判断GIF图片是否为动画的方法
Sep 04 PHP
ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例
Jul 22 PHP
php获取远程文件大小
Oct 20 PHP
yii2框架中使用下拉菜单的自动搜索yii-widget-select2实例分析
Jan 09 PHP
验证坐标在某坐标区域内php代码
Oct 08 PHP
PHP 获取指定地区的天气实例代码
Feb 08 PHP
PHP实现查询手机归属地的方法详解
Apr 28 PHP
php变量与JS变量实现不通过跳转直接交互的方法
Aug 25 PHP
ThinkPHP3.2框架自带分页功能实现方法示例
May 13 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实现分页的一个示例
2006/10/09 PHP
php中Socket创建与监听实现方法
2015/01/05 PHP
php基于jquery的ajax技术传递json数据简单实例
2016/04/15 PHP
IE6下js通过css隐藏select的一个bug
2010/08/16 Javascript
jQuery函数的等价原生函数代码示例
2013/05/27 Javascript
node.js中的http.response.getHeader方法使用说明
2014/12/14 Javascript
jquery中JSON的解析方式
2015/03/16 Javascript
JavaScript操作URL的相关内容集锦
2015/10/29 Javascript
前端js文件合并的三种方式推荐
2016/05/19 Javascript
JavaScript 继承详解(六)
2016/10/11 Javascript
微信小程序图表插件(wx-charts)实例代码
2017/01/17 Javascript
jQuery实现的简单悬浮层功能完整实例
2017/01/23 Javascript
基于匀速运动的实例讲解(侧边栏,淡入淡出)
2017/10/17 Javascript
微信小程序仿朋友圈发布动态功能
2018/07/15 Javascript
vue3.0 CLI - 1 - npm 安装与初始化的入门教程
2018/09/14 Javascript
jquery实现烟花效果(面向对象)
2020/03/10 jQuery
微信分享invalid signature签名错误踩过的坑
2020/04/11 Javascript
vue使用map代替Aarry数组循环遍历的方法
2020/04/30 Javascript
[01:12:27]EG vs Secret 2018国际邀请赛淘汰赛BO3 第二场 8.22
2018/08/23 DOTA
实例讲解Python设计模式编程之工厂方法模式的使用
2016/03/02 Python
Python基础之getpass模块详细介绍
2017/08/10 Python
Python之list对应元素求和的方法
2018/06/28 Python
Python函数参数操作详解
2018/08/03 Python
Python Selenium 之数据驱动测试的实现
2019/08/01 Python
深入浅析Python 中的sklearn模型选择
2019/10/12 Python
Python脚本如何在bilibili中查找弹幕发送者
2020/06/04 Python
京东港澳售:京东直邮港澳台
2018/01/31 全球购物
2014两会学习心得:榜样精神伴我行
2014/03/17 职场文书
创建青年文明号材料
2014/05/09 职场文书
校园环保建议书
2014/05/14 职场文书
离婚财产分隔协议书
2014/10/23 职场文书
申报优秀教师材料
2014/12/16 职场文书
2015国庆节66周年演讲稿
2015/03/20 职场文书
高中英语教学反思范文
2016/03/02 职场文书
Python requests库参数提交的注意事项总结
2021/03/29 Python
24年收藏2000多部退役军用电台
2022/02/18 无线电