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分页示例代码
Mar 19 PHP
PHP n个不重复的随机数生成代码
Jun 23 PHP
php 需要掌握的东西 不做浮躁的人
Dec 28 PHP
php flv视频时间获取函数
Jun 29 PHP
linux系统上支持php的 iconv()函数的方法
Oct 01 PHP
php 获取SWF动画截图示例代码
Feb 10 PHP
php中convert_uuencode()与convert_uuencode函数用法实例
Nov 22 PHP
php提交表单发送邮件的方法
Mar 20 PHP
WordPress的主题编写中获取头部模板和底部模板
Dec 28 PHP
WordPress后台中实现图片上传功能的实例讲解
Jan 11 PHP
php简单截取字符串代码示例
Oct 19 PHP
PHP框架自动加载类文件原理详解
Jun 06 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
基于mysql的论坛(4)
2006/10/09 PHP
同台服务器使用缓存APC效率高于Memcached的演示代码
2010/02/16 PHP
php trim 去除空字符的定义与语法介绍
2010/05/31 PHP
PHP __autoload函数(自动载入类文件)的使用方法
2012/02/04 PHP
php通过ajax实现双击table修改内容
2014/04/28 PHP
PHP微信开发之查询微信精选文章
2016/06/23 PHP
thinkphp 中的volist标签在ajax操作中的特殊性(推荐)
2018/01/15 PHP
PHP底层运行机制与工作原理详解
2020/07/31 PHP
PHP数组实际占用内存大小原理解析
2020/12/11 PHP
jquery自动将form表单封装成json的具体实现
2014/03/17 Javascript
jQuery过滤选择器:not()方法使用介绍
2014/04/20 Javascript
教你如何自定义百度分享插件以及bshare分享插件的分享按钮
2014/06/20 Javascript
Javascript基础知识(二)事件
2014/09/29 Javascript
Jquery搜索父元素操作方法
2015/02/10 Javascript
原生JS和jQuery版实现文件上传功能
2016/04/18 Javascript
基于jQuery实现顶部导航栏功能
2016/12/27 Javascript
Node.js和Express简单入门介绍
2017/03/24 Javascript
node.js-v6新版安装具体步骤(分享)
2017/09/06 Javascript
详解Vue 匿名、具名和作用域插槽的使用方法
2019/04/22 Javascript
javascript数组元素删除方法delete和splice解析
2019/12/09 Javascript
[06:33]3.19 DOTA2发布会 海涛、冷冷、2009见证希望
2014/03/21 DOTA
跟老齐学Python之集成开发环境(IDE)
2014/09/12 Python
python 使用get_argument获取url query参数
2017/04/28 Python
python实现媒体播放器功能
2018/02/11 Python
python sys.argv[]用法实例详解
2018/05/25 Python
PYTHON EVAL的用法及注意事项解析
2019/09/06 Python
如何在django中运行scrapy框架
2020/04/22 Python
美国求婚钻戒网站:Super Jeweler
2016/08/27 全球购物
汽车维修与检测专业应届生求职信
2013/11/12 职场文书
心得体会范文
2014/01/04 职场文书
四好少年事迹材料
2014/01/12 职场文书
党员学习中共十八大思想报告
2014/09/12 职场文书
2014年化验室工作总结
2014/11/21 职场文书
导游词怎么写
2015/02/04 职场文书
2015年政风行风工作总结
2015/04/21 职场文书
Golang二维数组的使用方式
2021/05/28 Golang