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使用数组实现队列
Feb 05 PHP
php 删除一个数组中的某个值.兼容多维数组!
Feb 18 PHP
php简单的留言板与回复功能具体实现
Feb 19 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(五)
Jun 23 PHP
PHP return语句的另一个作用
Jul 30 PHP
PHP实现图片旋转效果实例代码
Oct 01 PHP
Laravel 4 初级教程之Pages、表单验证
Oct 30 PHP
php截取html字符串及自动补全html标签的方法
Jan 15 PHP
thinkPHP交易详情查询功能详解
Dec 02 PHP
浅谈使用 Yii2 AssetBundle 中 $publishOptions 的正确姿势
Nov 08 PHP
详解PHP中mb_strpos的使用
Feb 04 PHP
Yii2框架加载css和js文件的方法分析
May 25 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
浅谈电磁辐射对健康的影响
2021/03/01 无线电
PHP 危险函数解释 分析
2009/04/22 PHP
ThinkPHP中session函数详解
2016/09/14 PHP
php中curl和soap方式请求服务超时问题的解决
2018/06/11 PHP
PHP+MySql实现一个简单的留言板
2020/07/19 PHP
JavaScript脚本性能的优化方法
2007/02/02 Javascript
Web开发者必备的12款超赞jQuery插件
2010/12/03 Javascript
使用CSS和jQuery模拟select并附提交后取得数据的代码
2013/10/18 Javascript
同域jQuery(跨)iframe操作DOM(示例代码)
2013/12/13 Javascript
删除javascript中注释语句的正则表达式
2014/06/11 Javascript
获取JS中网页各种高宽与位置的方法总结
2016/07/27 Javascript
微信小程序 教程之WXML
2016/10/18 Javascript
jQuery Ajax File Upload实例源码
2016/12/12 Javascript
AngularJS页面传参的5种方式
2017/04/01 Javascript
关于Vue实现组件信息的缓存问题
2017/08/23 Javascript
解决vue v-for 遍历循环时key值报错的问题
2018/09/06 Javascript
微信小程序人脸识别功能代码实例
2019/05/07 Javascript
[43:57]LGD vs Mineski 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
[47:31]完美世界DOTA2联赛PWL S3 INK ICE vs DLG 第一场 12.12
2020/12/16 DOTA
Java Web开发过程中登陆模块的验证码的实现方式总结
2016/05/25 Python
简单讲解Python编程中namedtuple类的用法
2016/06/21 Python
Python3爬虫之自动查询天气并实现语音播报
2019/02/21 Python
python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)
2019/08/28 Python
使用Python给头像戴上圣诞帽的图像操作过程解析
2019/09/20 Python
使用HTML5的Notification API制作web通知的教程
2015/05/08 HTML / CSS
LVMH旗下最大的奢侈品网站平台:24S
2020/05/24 全球购物
社团文化节邀请函
2014/01/10 职场文书
优秀员工获奖感言
2014/03/01 职场文书
好听的队名和口号
2014/06/09 职场文书
2014年后勤工作总结范文
2014/12/16 职场文书
2015年敬老月活动总结
2015/03/27 职场文书
大学同学聚会感言
2015/07/30 职场文书
健康教育主题班会
2015/08/14 职场文书
2019年员工晋升管理制度范本!
2019/07/08 职场文书
MySQL8.0.18配置多主一从
2021/06/21 MySQL
总结Pyinstaller打包的高级用法
2021/06/28 Python