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手册及PHP编程标准
Dec 17 PHP
php各种编码集详解和以及在什么情况下进行使用
Sep 11 PHP
php替换超长文本中的特殊字符的函数代码
May 22 PHP
curl实现站外采集的方法和技巧
Jan 31 PHP
codeigniter自带数据库类使用方法说明
Mar 25 PHP
PHP中文乱码解决方案
Mar 05 PHP
一个简单的php路由类
May 29 PHP
Laravel框架中自定义模板指令总结
Dec 17 PHP
php实现数字补零的方法总结
Sep 12 PHP
PHP中“=&gt;
Mar 01 PHP
Mac下快速搭建PHP开发环境步骤详解
May 05 PHP
详解no input file specified 三种解决方法
Nov 29 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
PHP5权威编程阅读学习笔记 附电子书下载
2012/07/05 PHP
无JS,完全php面向过程数据分页实现代码
2012/08/27 PHP
php正则替换处理HTML页面的方法
2015/06/17 PHP
Yii2.0高级框架数据库增删改查的一些操作
2015/11/16 PHP
php导出生成word的方法
2015/12/25 PHP
php基于PDO实现功能强大的MYSQL封装类实例
2017/02/27 PHP
php从数据库读取数据,并以json格式返回数据的方法
2018/08/21 PHP
把textarea中字符串里含有的回车换行替换成&amp;lt;br&amp;gt;的javascript代码
2007/04/20 Javascript
有关JavaScript的10个怪癖和秘密分享
2011/08/28 Javascript
jquery京东商城双11焦点图多图广告特效代码分享
2015/09/06 Javascript
基于jQuery实现的扇形定时器附源码下载
2015/10/20 Javascript
Angularjs material 实现搜索框功能
2016/03/08 Javascript
简介BootStrap model弹出框的使用
2016/04/27 Javascript
JS判断字符串变量是否含有某个字串的实现方法
2016/06/03 Javascript
ES6新特性六:promise对象实例详解
2017/04/21 Javascript
妙用Angularjs实现表格按指定列排序
2017/06/23 Javascript
基于vue实现分页效果
2017/11/06 Javascript
JS实现去除数组中重复json的方法示例
2017/12/21 Javascript
jQuery.extend 与 jQuery.fn.extend的用法及区别实例分析
2018/07/25 jQuery
vue异步axios获取的数据渲染到页面的方法
2018/08/09 Javascript
原生JS实现的简单轮播图功能【适合新手】
2018/08/17 Javascript
微信小程序如何实现在线客服功能
2019/10/16 Javascript
python全栈要学什么 python全栈学习路线
2019/06/28 Python
Pandas的read_csv函数参数分析详解
2019/07/02 Python
python输出数组中指定元素的所有索引示例
2019/12/06 Python
wxpython自定义下拉列表框过程图解
2020/02/14 Python
pytorch:model.train和model.eval用法及区别详解
2020/02/20 Python
自荐书4要点
2014/01/25 职场文书
倡议书范文
2014/04/16 职场文书
中学生教师节演讲稿
2014/09/03 职场文书
党员批评与自我批评材料
2014/10/14 职场文书
党员批评与自我批评发言稿
2014/10/14 职场文书
2015新年寄语大全
2014/12/08 职场文书
飞屋环游记观后感
2015/06/08 职场文书
Python字符串常规操作小结
2022/04/03 Python
Windows Server 2012 R2 磁盘分区教程
2022/04/29 Servers