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 相关文章推荐
PHP4在Windows2000下的安装
Oct 09 PHP
smarty 原来也不过如此~~呵呵
Nov 25 PHP
php下将图片以二进制存入mysql数据库中并显示的实现代码
May 27 PHP
解析PHP汉字转换拼音的类
Jun 18 PHP
PHP利用func_get_args和func_num_args函数实现函数重载实例
Nov 12 PHP
PHP采集静态页面并把页面css,img,js保存的方法
Dec 23 PHP
php单例模式实现方法分析
Mar 14 PHP
Thinkphp框架开发移动端接口(2)
Aug 18 PHP
laravel5.4利用163邮箱发送邮件的步骤详解
Sep 22 PHP
PHP xpath()函数讲解
Feb 11 PHP
php用户名的密码加密更安全的方法
Jun 21 PHP
php无限极分类实现方法分析
Jul 04 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
一个很方便的 XML 类!!原创的噢
2006/10/09 PHP
php生成缩略图的类代码
2008/10/02 PHP
PHP基于关联数组20行代码搞定约瑟夫问题示例
2017/11/07 PHP
php设计模式之原型模式分析【星际争霸游戏案例】
2020/03/23 PHP
UI Events 用户界面事件
2012/06/27 Javascript
web网页按比例显示图片实现原理及js代码
2013/08/09 Javascript
浅析js中的浮点型运算问题
2014/01/06 Javascript
jquery选择符快速提取web表单数据示例
2014/03/27 Javascript
JavaScript AJAX之惰性载入函数
2014/08/27 Javascript
node.js中的fs.rmdir方法使用说明
2014/12/16 Javascript
JS获取和修改元素样式的实例代码
2016/08/06 Javascript
详解Vue.js动态绑定class
2016/12/20 Javascript
ThinkPHP+jquery实现“加载更多”功能代码
2017/03/11 Javascript
微信小程序实现YDUI的ScrollTab组件
2018/02/02 Javascript
详解vue2.0+axios+mock+axios-mock+adapter实现登陆
2018/07/19 Javascript
详解vscode中vue代码颜色插件
2018/10/11 Javascript
Javascript原型链及instanceof原理详解
2020/05/25 Javascript
详解三种方式在React中解决绑定this的作用域问题并传参
2020/08/18 Javascript
在HTML中使用JavaScript的两种方法
2020/12/24 Javascript
[45:06]完美世界DOTA2联赛PWL S2 Magma vs InkIce 第二场 11.28
2020/12/02 DOTA
以Flask为例讲解Python的框架的使用方法
2015/04/29 Python
python实现一个简单的ping工具方法
2019/01/31 Python
感知器基础原理及python实现过程详解
2019/09/30 Python
Python3 解决读取中文文件txt编码的问题
2019/12/20 Python
pandas apply使用多列计算生成新的列实现示例
2021/02/24 Python
HTML5的结构和语义(4):语义性的内联元素
2008/10/17 HTML / CSS
Hobbs官方网站:英国奢华女性时尚服装
2020/02/22 全球购物
新西兰购物网站:TheMarket NZ
2020/09/19 全球购物
WEB控件可以激发服务端事件,请谈谈服务端事件是怎么发生并解释其原理?自动传回是什么?为什么要使用自动传回?
2012/02/21 面试题
宿舍打麻将检讨书
2014/01/24 职场文书
驾驶员培训方案
2014/05/01 职场文书
2015年高考寄语或鼓励的话
2015/03/23 职场文书
社区重阳节活动总结
2015/03/24 职场文书
力克胡哲观后感
2015/06/10 职场文书
学校运动会感想
2015/08/10 职场文书
mysql 获取相邻数据项
2022/05/11 MySQL