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+mysql开源XNA 聚合程序发布 下载
Jul 13 PHP
php+mysql事务rollback&amp;commit示例
Feb 08 PHP
PHP strtok()函数的优点分析
Mar 02 PHP
PHP 获取远程文件大小的3种解决方法
Jul 11 PHP
PHP使用递归方式列出当前目录下所有文件的方法
Jun 02 PHP
PHP 前加at符合@的作用解析
Jul 31 PHP
购物车实现的几种方式优缺点对比
May 02 PHP
Laravel 自动生成验证的实例讲解:login / logout
Oct 14 PHP
laravel 数据验证规则详解
Oct 23 PHP
在 Laravel 6 中缓存数据库查询结果的方法
Dec 11 PHP
php设计模式之观察者模式实例详解【星际争霸游戏案例】
Mar 30 PHP
PHP基于进程控制函数实现多线程
Dec 09 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 Class 文章
2007/04/04 PHP
VIM中设置php自动缩进为4个空格的方法详解
2013/06/14 PHP
js常见表单应用技巧
2008/01/09 Javascript
javascript 模拟坦克大战游戏(html5版)附源码下载
2014/04/08 Javascript
Javascript核心读书有感之语言核心
2015/02/01 Javascript
JS选中checkbox后获取table内一行TD所有数据的方法
2015/07/01 Javascript
JavaScript测试工具之Karma-Jasmine的安装和使用详解
2015/12/03 Javascript
jquery 将当前时间转换成yyyymmdd格式的实现方法
2016/06/01 Javascript
JavaScript实战(原生range和自定义特效)简单实例
2016/08/21 Javascript
vue2.0开发实践总结之疑难篇
2016/12/07 Javascript
读Javascript高性能编程重点笔记
2016/12/21 Javascript
bootstrap中的 form表单属性role=&quot;form&quot;的作用详解
2017/01/20 Javascript
angularJS 指令封装回到顶部示例详解
2017/01/22 Javascript
学习使用Bootstrap页面排版样式
2017/05/11 Javascript
Angular实现下载安装包的功能代码分享
2017/09/05 Javascript
jQuery位置选择器用法实例分析
2019/06/28 jQuery
angular6开发steps步骤条组件
2019/07/04 Javascript
解决vue-cli 打包后自定义动画未执行的问题
2019/11/12 Javascript
vue 图片裁剪上传组件的实现
2020/11/12 Javascript
python定时检查启动某个exe程序适合检测exe是否挂了
2013/01/21 Python
python实现QQ邮箱/163邮箱的邮件发送
2019/01/22 Python
Django框架安装方法图文详解
2019/11/04 Python
Python使用for生成列表实现过程解析
2020/09/22 Python
python获取linux系统信息的三种方法
2020/10/14 Python
荷兰演唱会和体育比赛订票网站:viagogo荷兰
2018/04/08 全球购物
人事专员职责
2014/02/22 职场文书
《卖木雕的少年》教学反思
2014/04/11 职场文书
禁毒宣传工作方案
2014/05/23 职场文书
公司门卫工作职责
2014/06/28 职场文书
群教班子对照检查材料
2014/08/26 职场文书
公司开业致辞
2015/07/29 职场文书
2016年情人节广告语
2016/01/28 职场文书
导游词之吉林花园山
2019/10/17 职场文书
Spring Boot 实现敏感词及特殊字符过滤处理
2021/06/29 Java/Android
动视暴雪取消疫苗禁令 让所有员工返回线下工作
2022/04/03 其他游戏
JS前端轻量fabric.js系列物体基类
2022/08/05 Javascript