+ -
当前位置:首页 → 问答吧 → [无聊]一个简单创建XML的Class

[无聊]一个简单创建XML的Class

时间:2007-02-09

来源:互联网

复制内容到剪贴板
代码:
<?php
/**
XML类:用于创建简单的XML文档,暂不支持命名空间和Style.
版本:0.3 Beta
作者:lazy
邮件:o0lazy0o at gmail dot com
版权:无,可任意使用;如果可以请保留此信息,如您对它做出修改及添加功能请发一个拷贝到我的邮件给我,谢谢!
Welcome to http://www.52radio.net/
类成员说明:
$Content变量用于保存XML文档
$RootNode变量保存根节点(一个XML文档只能有一个根节点)
$ParentNode变量保存父节点
---------------------------------------------------------------
例子:
$XML=new XML();
$Attribute['date']=date('Y-m-d');
$Attribute['time']=date('H:i:s');
$XML->CreateNode('DocumentROOT',$Attribute);
$Att['author']='Lazy';
$XML->AppendNode('Parent',$Att);
$XML->AppendNode('Child',$Att,'this is text test node.',false);
$XML->AppendNode('Child',$Att,'this is text test node.',false);
$XML->AppendNode('Child',$Att,'this is text test node.',false);
$XML->AppendNode('Child',$Att,'this is text test node.',false);
$Att['Email']='[email protected]';
$XML->AppendNode('ParentII',$Att);
$XML->AppendNode('Child',null,'this is text test node.',false);
$XML->AppendNode('Child',null,'this is text test node.',false);
$XML->AppendNode('Child',null,'this is text test node.',false);
$XML->AppendNode('Child',null,'this is text test node.',false);
$XML->Save('example.xml');
$XML->Display();
---------------------------------------------------------------
运行结果:
<?xml version="1.0" encoding="utf-8"?>
<DocumentROOT date='2006-12-24' time='21:23:59'>
<Parent author='Lazy'>
<Child author='Lazy'>this is text test node.</Child>
<Child author='Lazy'>this is text test node.</Child>
<Child author='Lazy'>this is text test node.</Child>
<Child author='Lazy'>this is text test node.</Child>
</Parent>
<ParentII author='Lazy' Email='[email protected]'>
<Child>this is text test node.</Child>
<Child>this is text test node.</Child>
<Child>this is text test node.</Child>
<Child>this is text test node.</Child>
</ParentII>
</DocumentROOT>
---------------------------------------------------------------
*/
Class XML{
    var $Content="";
    var $RootNode="";
    var $ParentNode="";
    var $CRLF="\r\n";
    var $End="";
   
    /**
     * 构造函数:生成默认XML头
     * 返回值:true
     */
    Function XML($Version="1.0",$Encoding="utf-8"){
        $this->Content.="<?xml version=\"{$Version}\" encoding=\"{$Encoding}\"?>{$this->CRLF}";
    }

    /**
     * CreateNode函数:创建根节点
     * $NodeName为节点名称
     * $Attribute为节点的属性
     * 返回值为处理后的内容
     */
    Function CreateNode($NodeName="root",$Attribute=""){
        $NodeName=$this->Filter($NodeName);
        $this->RootNode=$NodeName;
        $Attribute=$this->ParseAttribute($Attribute);
        return $this->Content.="<{$NodeName}{$Attribute}>{$this->CRLF}";
    }

    /**
     * AppendNode函数:添加节点
     * 如果没有数据的话为父节点,有为带数据的节点
     * $NodeName为节点名称
     * $Attribute为节点的属性
     * 返回值为处理后的内容
     */
    Function AppendNode($NodeName,$Attribute,$Data="",$CDate=true){
        $NodeName=$this->Filter($NodeName);
        if(empty($Data)){
            if(!empty($this->ParentNode)){
                $this->Content.="</{$this->ParentNode}>{$this->CRLF}";
            }
            $this->ParentNode=$NodeName;
            $Attribute=$this->ParseAttribute($Attribute);
            return $this->Content.="<{$NodeName}{$Attribute}>{$this->CRLF}";
        }else{
            $Attribute=$this->ParseAttribute($Attribute);
            return $this->Content.=$CDate?"<{$NodeName}{$Attribute}>{$this->CRLF}<![CDATA[{$Data}]]>{$this->CRLF}</{$NodeName}>{$this->CRLF}":"<{$NodeName}{$Attribute}>{$Data}</{$NodeName}>{$this->CRLF}";
        }
    }
   
    /**
     * End函数:添加XML结束节点字符串
     * 返回完整的XML文档内容
     */
    Function End(){
        if($this->End){
            return $this->Content;
        }else{
            $this->End=true;
            return $this->Content=$this->ParentNode==""?$this->Content."</{$this->RootNode}>":$this->Content."</{$this->ParentNode}>{$this->CRLF}</{$this->RootNode}>";
        }
    }

    /**
     * Display函数:输出XML文档
     * 无返回值
     */
    Function Display(){
        ob_start();
        header("Content-type: text/xml");
        echo $this->End();
        ob_end_flush();
    }

    /**
     * Save函数:保存XML文档
     * 保存路径默认为程序当前目录,可在$Filename指定路径如:SITEROOT."/archives/save.xml"(注意权限问题)
     * 返回值:true或者false
     */
    Function Save($Filename){
        if(!$Handle=fopen($Filename,'wb+')){
            $this->Error('不能创建文件或写入文件请检查权限');
        }
        flock($Handle,LOCK_EX);
        fwrite($Handle,$this->End());
        return fclose($Handle);
    }

    /**
     * Error函数:处理错误信息
     */
    Function Error($ErrorStr='',$ErrorNo='',$Stop=true){
        exit($ErrorStr);
    }

    /**
     * ParseAttribute函数:解析节点属性数组
     * $Argv为要解析的内容数组,注意此数组只能为一维数组
     * 返回值为解析过后的内容
     */
    Function ParseAttribute($Argv){
        $Attribute='';
        if(is_array($Argv)){
            foreach($Argv as $Key=>$Value){
                if(is_array($Value)){
                    $this->Error('属性错误:存放属性的数组只能是一维数组');
                }
                $Value=$this->Filter($Value);
                $Attribute.=" $Key=\"$Value\"";
            }
        }
        return $Attribute;
    }
   
    /**
     * Filter函数:过滤字符
     * 返回处理完的内容
     */
    Function Filter($Argv){
        $Argv=trim($Argv);
        $Search=array("<",">","\"");
        $Replace=array("","","'");
        return str_replace($Search,$Replace,$Argv);
    }
}
?>

作者: lazy   发布时间: 2007-02-09

顶个沙发

作者: cator   发布时间: 2007-02-09

坐上板凳看~

作者: airwin   发布时间: 2007-02-10

先顶在说

作者: wwwfk   发布时间: 2007-02-10

这个都要写成类???

作者: fnet   发布时间: 2007-02-14

热门下载

更多