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
PHP个人网站架设连环讲(四)
Oct 09 PHP
php中ob(Output Buffer 输出缓冲)函数使用方法
Jul 21 PHP
php若干单维数组遍历方法的比较
Sep 20 PHP
PHP 实现explort() 功能的详解
Jun 20 PHP
JoshChen_web格式编码UTF8-无BOM的小细节分析
Aug 16 PHP
PHP把数字转成人民币大写的函数分享
Jun 30 PHP
phpmyadmin出现Cannot start session without errors问题解决方法
Aug 14 PHP
完美解决Thinkphp3.2中插入相同数据的问题
Aug 01 PHP
PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法
Nov 10 PHP
PHP实现的XXTEA加密解密算法示例
Aug 28 PHP
PHP For循环字母A-Z当超过26个字母时输出AA,AB,AC
Feb 16 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中MVC的开发经验分享
2012/05/17 PHP
PHP表单数据写入MySQL数据库的代码
2016/05/31 PHP
详解ThinkPHP3.2.3验证码显示、刷新、校验
2016/12/29 PHP
PHP实现动态压缩js与css文件的方法
2018/05/02 PHP
jquery 显示*天*时*分*秒实现时间计时器
2014/05/07 Javascript
javascript查询字符串参数的方法
2015/01/28 Javascript
学习JavaScript设计模式(继承)
2015/11/26 Javascript
JS实现的简单轮播图运动效果示例
2016/12/22 Javascript
浅谈函数调用的不同方式,以及this的指向
2017/09/17 Javascript
浅谈JS函数节流防抖
2017/10/18 Javascript
关于HTTP传输中gzip压缩的秘密探索分析
2018/01/12 Javascript
详解vue.js根据不同环境(正式、测试)打包到不同目录
2018/07/13 Javascript
记一次webapck4 配置文件无效的解决历程
2018/09/19 Javascript
JS实现的定时器展示简单秒表、页面弹框及跳转操作完整示例
2020/01/26 Javascript
jQuery实现聊天对话框
2020/02/08 jQuery
js实现百度登录窗口拖拽效果
2020/03/19 Javascript
JavaScript实现网页计算器功能
2020/10/29 Javascript
[00:43]FTP典藏礼包 DOTA2三大英雄霸气新套装
2014/03/21 DOTA
[16:56]教你分分钟做大人:司夜刺客
2014/10/30 DOTA
python分割和拼接字符串
2013/11/01 Python
通过Python爬虫代理IP快速增加博客阅读量
2016/12/14 Python
Python 结巴分词实现关键词抽取分析
2017/10/21 Python
Python使用Flask-SQLAlchemy连接数据库操作示例
2018/08/31 Python
django富文本编辑器的实现示例
2019/04/10 Python
python3+PyQt5 实现Rich文本的行编辑方法
2019/06/17 Python
Python实现最大子序和的方法示例
2019/07/05 Python
python实现在函数图像上添加文字和标注的方法
2019/07/08 Python
python tkinter控件布局项目实例
2019/11/04 Python
详解Django admin高级用法
2019/11/06 Python
用CSS3实现背景渐变的方法
2015/07/14 HTML / CSS
公务员培训自我鉴定
2013/09/19 职场文书
销售主管的自我评价分享
2014/01/03 职场文书
大学学习个人的自我评价
2014/02/18 职场文书
奠基仪式主持词
2014/03/20 职场文书
商务日语专业毕业生自荐信
2014/03/27 职场文书
党课培训心得体会
2014/09/02 职场文书