3


Posted in PHP onOctober 09, 2006

{
                    if (is_array($value) && ! $delete)
                    {
                        foreach ($value as $suboption => $subvalue)
                        {
                            $this->{$option}["$suboption"] = $subvalue;
                        }
                    }
                    else
                    {
                          $this->$option = $value;
                    }
                }
            }
        }
    }

    // these are the functions, which are intended to be overriden in user classes

    /**
    *
    * @param    mixed
    * @return   object  DomNode
    * @access   private
    */
    function insertNewResult(&$metadata)
    {
        if ($this->xmlroot)
            return $this->xmlroot->new_child($this->tagNameResult, NULL);
        else
        {
            $this->xmlroot = $this->xmldoc->add_root($this->tagNameResult);
            //PHP 4.0.6 had $root->name as tagname, check for that here...
            if (!isset($this->xmlroot->{$this->tagname}))
            {
                $this->tagname = "name";
            }
            return $this->xmlroot;

        }
    }

    /**
    *   to be written
    *
    * @param    object DomNode $parent_row
    * @param    mixed $res
    * @param    mixed $key
    * @param    mixed &metadata
    * @return   object DomNode
    * @access private
    */
    function insertNewRow($parent_row, $res, $key, &$metadata)
    {
        return  $parent_row->new_child($this->tagNameRow, Null);
    }

    /**
    *   to be written
    *
    * @param    object DomNode $parent
    * @param    mixed $res
    * @param    mixed $key
    * @param    mixed &$metadata
    * @param    mixed &$subrow
    * @return   object DomNode
    * @access private
    */
    function insertNewElement($parent, $res, $key, &$metadata, &$subrow)
    {
        return  $parent->new_child($metadata[$key]["name"], $this->xml_encode(trim$res[$key]));
    }

    /**
    *   to be written
    *
    * @param    mixed $key
    * @param    mixed $value
    * @param    mixed &$metadata
    * @access private
    */
    function addTableInfo($key, $value, &$metadata) {

    }

    // end functions, which are intended to be overriden in user classes

    // here come some helper functions...

    /**
    * make utf8 out of the input data and escape & with & and "< " with "< "
    * (we assume that when there's no space after < it's a tag, which we need in the xml)
    *  I'm not sure, if this is the standard way, but it works for me.
    *
    * @param    string text to be utfed.
    * @access private
    */
    function xml_encode ($text)
    {
        if (function_exists("iconv") && isset($this->encoding_from) && isset($this->encoding_to))
        {
             ini_set("track_errors",1);
             $text = iconv($this->encoding_from,$this->encoding_to,ereg_replace("&","&",ereg_replace("< ","< ",$text)));

             if (! isset($text) )
             {
                if (isset($php_errormsg))
                {
                    $errormsg = "error: $php_errormsg";
                }
                else
                {
                    $errormsg = "undefined iconv error, turn on track_errors in php.ini to get more details";
                }
                return PEAR::raiseError($errormsg,Null,PEAR_ERROR_DIE);
             }
             else {
                return $text;
             }
        }
        else
        {
            //$text = utf8_encode(ereg_replace("&","&",ereg_replace("< ","< ",$text)));
            $text = trim(ereg_replace("&","&",ereg_replace("< ","< ",$text)));
//            echo $text;
        }
        return $text;
    }

    //taken from kc@hireability.com at http://www.php.net/manual/en/function.array-merge-recursive.php
    /**
    * There seemed to be no built in function that would merge two arrays recursively and clobber
    *   any existing key/value pairs. Array_Merge() is not recursive, and array_merge_recursive
    *   seemed to give unsatisfactory results... it would append duplicate key/values.
    *
    *   So here's a cross between array_merge and array_merge_recursive
    **/
    /**
    *
    * @param    array first array to be merged
    * @param    array second array to be merged
    * @return   array merged array
    * @access private
    */
    function array_merge_clobber($a1,$a2)
    {
        if(!is_array($a1)
!is_array($a2)) return false;
        $newarray = $a1;
        while (list($key, $val) = each($a2))
        {
            if (is_array($val) && is_array($newarray[$key]))
            {
                $newarray[$key] = $this->array_merge_clobber($newarray[$key], $val);
            }
            else
            {
                $newarray[$key] = $val;
            }
        }
        return $newarray;
    }

    /**
    * Adds a xml string to $this->xmldoc.
    * It's inserted on the same level as a "normal" resultset, means just as a children of <root>
    * if a xpath expression is supplied, it takes that for selecting only part of the xml-file
    *
    * the clean code works only with php 4.0.7
    * for php4.0.6 :
    * I found no cleaner method than the below one. it's maybe nasty (xmlObject->string->xmlObject),
    *  but it works. If someone knows how to add whole DomNodes to another one, let me know...
    *
    * @param    string xml string
    * @param    mixed xpath  either a string with the xpath expression or an array with "xpath"=>xpath expression  and "root"=tag/subtag/etc, which are the tags to be inserted before the result
    * @access private
    */

    function doXmlString2Xml ($string,$xpath = Null)
    {

        //check if we have a recent domxml. otherwise use the workaround...
        $version = explode(".",phpversion());

        if (! ($version[0] <= 4 and $version[1] <= 0 and $version[2] < 7) ){

            if (is_array($xpath))
            {
                if (isset($xpath["root"]))
                {
                    $root = $xpath["root"];
                }
                $xpath = $xpath["xpath"];
            }

            $tmpxml = xmldoc($string);
            $subroot = $this->xmlroot;

            if (isset($root))
            {
                $roots = explode("/",$root);
                foreach ($roots as $rootelement)
                {
                    if ( strlen($rootelement) > 0 )
                    {
                        $subroot = $subroot->new_child($rootelement,"");
                    }
                }
            }

            //$this->xmlroot->addchild does some strange things when added nodes from xpath.... so this comment helps out
            $newchild = $subroot->add_child($this->xmldoc->create_comment("the purpose of this comment is a workaround in sql2php.php line ".__LINE__));

            // if no xpath is given, just take the whole file
            if ( (is_null($xpath)))
            {
                $newchild->append_child($tmpxml->root());
            }
            else
            {
                $xctx = $tmpxml->xpath_new_context();
                $xnode = xpath_eval($xctx,$xpath);
                foreach ($xnode->nodeset as $node)
                {
                    $newchild->append_child($node);
                }
            }

         }
        else {
            $MainXmlString = $this->xmldoc->dumpmem();
            $string = preg_replace("/<\?xml.*\?>/","",$string);

            $MainXmlString = preg_replace("/<".$this->xmlroot->{$this->tagname}."\/>/","<".$this->xmlroot->{$this->tagname}."></".$this->xmlroot->{$this->tagname}.">",$MainXmlString);
            $MainXmlString = preg_replace("/<\/".$this->xmlroot->{$this->tagname}.">/",$string."</".$this->xmlroot->{$this->tagname}.">",$MainXmlString);

            $this->xmldoc = xmldoc($MainXmlString);
            $this->xmlroot = $this->xmldoc->root();

        }
    }

    /**
    * sets the encoding for the db2xml transformation
    * @param    string $encoding_from encoding to transform from
    * @param    string $encoding_to encoding to transform to
    * @access public
    */
    function setEncoding ($encoding_from = "ISO-8859-1", $encoding_to ="UTF-8")
    {
        $this->encoding_from = $encoding_from;
        $this->encoding_to = $encoding_to;
    }
    /**
    * @param array $parentTables parent to child relation
    * @access public
    */

    function SetParentTables($parentTables)
    {
        foreach ($parentTables as $table => $parent)
        {
            $table_info["parent_table"][$table]=$parent;
        }
        $this->SetOptions(array("user_tableInfo"=>$table_info));
    }

    /**
    * returns the content of the first match of the xpath expression
    *
    * @param    string $expr xpath expression
    * @return   mixed content of the evaluated xpath expression
    * @access   public
    */

    function getXpathValue ($expr)
    {

        $xpth = $this->xmldoc->xpath_new_context();
        $xnode = xpath_eval($xpth,$expr);

        if (isset ($xnode->nodeset[0]))
        {
            $firstnode = $xnode->nodeset[0];

            $children = $firstnode->children();
            $value = $children[0]->content;
                return $value;
        }

        else
        {
            return Null;
        }
    }

    /**
    * get the values as an array from the childtags from the first match of the xpath expression
    *
    * @param    string xpath expression
    * @return   array with key->value of subtags
    * @access   public
    */

    function getXpathChildValues ($expr)
    {
        $xpth = $this->xmldoc->xpath_new_context();
        $xnode = xpath_eval($xpth,$expr);

        if (isset ($xnode->nodeset[0]))
        {
            foreach ($xnode->nodeset[0]->children() as $child)
            {
                $children = $child->children();
                $value[$child->{$this->tagname}] = $children[0]->content;
            }
            return $value;
        }
        else
        {
            return Null;
        }
    }

}
?>

PHP 相关文章推荐
PHP中路径问题的解决方案
Oct 09 PHP
BBS(php &amp; mysql)完整版(五)
Oct 09 PHP
php相当简单的分页类
Oct 02 PHP
PHP setTime 设置当前时间的代码
Aug 27 PHP
解析htaccess伪静态的规则
Jun 18 PHP
php 使用GD库为页面增加水印示例代码
Mar 24 PHP
浅析PHP中strlen和mb_strlen的区别
Aug 31 PHP
laravel容器延迟加载以及auth扩展详解
Mar 02 PHP
php短网址和数字之间相互转换的方法
Mar 13 PHP
PHP面试题之文件目录操作
Oct 15 PHP
PHP用户管理中常用接口调用实例及解析(含源码)
Mar 09 PHP
基于php伪静态的实现方法解析
Jul 31 PHP
example2.php
Oct 09 #PHP
对盗链说再见...
Oct 09 #PHP
PHP出错界面
Oct 09 #PHP
ftp类(myftp.php)
Oct 09 #PHP
第1次亲密接触PHP5(2)
Oct 09 #PHP
PHP的FTP学习(一)[转自奥索]
Oct 09 #PHP
一个多文件上传的例子(原创)
Oct 09 #PHP
You might like
PHP安装全攻略:APACHE
2006/10/09 PHP
PHP中将ip地址转成十进制数的两种实用方法
2013/08/15 PHP
Yii2 rbac权限控制之菜单menu实例教程
2016/04/28 PHP
PHP7新功能总结
2019/04/14 PHP
thinkPHP+LayUI 流加载实现功能
2019/09/27 PHP
thinkPHP事务操作简单案例分析
2019/10/17 PHP
Laravel 6.2 中添加了可调用容器对象的方法
2019/10/22 PHP
jQuery .tmpl(), .template()学习资料小结
2011/07/18 Javascript
JQuery中关于jquery.js与jquery.min.js的比较探讨
2013/05/15 Javascript
javascript引擎长时间独占线程造成卡顿的解决方案
2014/12/03 Javascript
js判断浏览器类型及设备(移动页面开发)
2015/07/30 Javascript
JavaScript第一篇之实现按钮全选、功能
2016/08/21 Javascript
JavaScript实现窗口抖动效果
2016/10/19 Javascript
JS实现的模仿QQ头像资料卡显示与隐藏效果
2017/04/07 Javascript
Angular中ng-repeat与ul li的多层嵌套重复问题
2017/07/24 Javascript
JavaScript面向对象精要(下部)
2017/09/12 Javascript
javascript中函数的写法实例代码详解
2018/10/28 Javascript
如何基于JS截获动态代码
2019/12/25 Javascript
vant实现购物车功能
2020/06/29 Javascript
vue 授权获取微信openId操作
2020/11/13 Javascript
Python数据处理numpy.median的实例讲解
2018/04/02 Python
python使用pip安装模块出现ReadTimeoutError: HTTPSConnectionPool的解决方法
2019/10/04 Python
使用Python函数进行模块化的实现
2019/11/15 Python
python标准库os库的函数介绍
2020/02/12 Python
浅析matlab中imadjust函数
2020/02/27 Python
Django 解决由save方法引发的错误
2020/05/21 Python
使用Pycharm在运行过程中,查看每个变量的操作(show variables)
2020/06/08 Python
Alpine安装Python3依赖出现的问题及解决方法
2020/12/25 Python
HTML5等待加载动画效果
2017/07/27 HTML / CSS
html5应用缓存_动力节点Java学院整理
2017/07/13 HTML / CSS
公立医院改革实施方案
2014/03/14 职场文书
办公设备采购方案
2014/03/16 职场文书
离职证明范本(5篇)
2014/09/19 职场文书
浅谈JS的二进制家族
2021/05/09 Javascript
【DOTA2】当街暴打?PSG LGD vs VG - DPC 2022 WINTER TOUR CN
2022/04/02 DOTA
MySQL 计算连续登录天数
2022/05/11 MySQL