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 替换模板变量实现步骤
Aug 24 PHP
php 验证码实例代码
Jun 01 PHP
php中记录用户访问过的产品,在cookie记录产品id,id取得产品信息
May 04 PHP
ThinkPHP3.2.3数据库设置新特性
Mar 05 PHP
PHP实现阳历到农历转换的类实例
Mar 07 PHP
php在数组中查找指定值的方法
Mar 17 PHP
php给一组指定关键词添加span标签的方法
Mar 31 PHP
编写PHP脚本来实现WordPress中评论分页的功能
Dec 10 PHP
PHP读取文本文件并逐行输出该行使用最多的字符与对应次数的方法
Nov 25 PHP
Laravel 的数据库迁移的方法
Jul 31 PHP
PHP迭代与递归实现无限级分类
Aug 28 PHP
PHP实现简单的模板引擎功能示例
Sep 02 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类中Static方法效率测试代码
2010/10/17 PHP
php使用PDO获取结果集的方法
2017/02/16 PHP
PHP实现根据密码长度显示安全条
2017/07/04 PHP
thinkPHP微信分享接口JSSDK用法实例
2017/07/07 PHP
PHP设计模式之模板模式定义与用法详解
2018/12/20 PHP
限制文本字节数js代码
2007/03/06 Javascript
jquery 学习之一 对象访问
2010/11/23 Javascript
引用外部js乱码问题分析及解决方案
2013/04/12 Javascript
如何调试异步加载页面里包含的js文件
2014/10/30 Javascript
jQuery中:nth-child选择器用法实例
2014/12/31 Javascript
JS实现在线统计一个页面内鼠标点击次数的方法
2015/02/28 Javascript
javascript实现延时显示提示框效果
2017/06/01 Javascript
jQuery初级教程之网站品牌列表效果
2017/08/02 jQuery
微信小程序实现多宫格抽奖活动
2020/04/15 Javascript
angular4中*ngFor不能对返回来的对象进行循环的解决方法
2018/09/12 Javascript
谈谈React中的Render Props模式
2018/12/06 Javascript
JS简单判断是否在微信浏览器打开的方法示例
2019/01/08 Javascript
微信小程序上传图片并等比列压缩到指定大小的实例代码
2019/10/24 Javascript
JS如何在不同平台实现多语言方式
2020/07/16 Javascript
js实现移动端图片滑块验证功能
2020/09/29 Javascript
使用Node.js和Socket.IO扩展Django的实时处理功能
2015/04/20 Python
Python基于smtplib实现异步发送邮件服务
2015/05/28 Python
Python函数返回值实例分析
2015/06/08 Python
python列表使用实现名字管理系统
2019/01/30 Python
python的turtle库使用详解
2019/05/10 Python
django 实现celery动态设置周期任务执行时间
2019/11/19 Python
关键在于落实心得体会
2014/09/03 职场文书
正风肃纪查摆剖析材料
2014/10/10 职场文书
个人股份合作协议书
2014/10/24 职场文书
先进个人事迹材料
2014/12/29 职场文书
教师读书笔记
2015/06/29 职场文书
致毕业季:你如何做好自己的职业生涯规划书?
2019/07/01 职场文书
Python 如何安装Selenium
2021/05/06 Python
MySQL COUNT函数的使用与优化
2021/05/10 MySQL
Python实现日志实时监测的示例详解
2022/04/06 Python