cvs: php4 /pear/Experimental/XML sql2xml.php

From: Date: Fri, 30 Mar 2001 10:36:42 +0000
Subject: cvs: php4 /pear/Experimental/XML sql2xml.php
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-4914@lists.php.net to get a copy of this message
chregu Fri Mar 30 02:36:42 2001 EDT Modified files: /php4/pear/Experimental/XML sql2xml.php Log: - new user_tableInfo option for user-contributed xml-structure - Error checking in doSql2Xml - xml_encode function for correctly parsing of &s. Index: php4/pear/Experimental/XML/sql2xml.php diff -u php4/pear/Experimental/XML/sql2xml.php:1.2 php4/pear/Experimental/XML/sql2xml.php:1.3 --- php4/pear/Experimental/XML/sql2xml.php:1.2 Thu Mar 29 13:26:53 2001 +++ php4/pear/Experimental/XML/sql2xml.php Fri Mar 30 02:36:42 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@nomad.ch> | // +----------------------------------------------------------------------+ // -// $Id: sql2xml.php,v 1.2 2001/03/29 21:26:53 uw Exp $ +// $Id: sql2xml.php,v 1.3 2001/03/30 10:36:42 chregu Exp $ /** * This class takes a PEAR::DB-Result Object (or more than one) @@ -37,7 +37,7 @@ * for the time being * * @author Christian Stocker <chregu@nomad.ch> -* @version $Id: sql2xml.php,v 1.2 2001/03/29 21:26:53 uw Exp $ +* @version $Id: sql2xml.php,v 1.3 2001/03/30 10:36:42 chregu Exp $ */ class XML_sql2xml { @@ -71,7 +71,14 @@ /** * + * @var boolean + */ + var $user_tableInfo = False; + + + /** * + * * @param string */ function XML_sql2xml ($root = "root") { @@ -139,6 +146,11 @@ function doSql2Xml($result, $options = False) { + if (DB::IsError($result)) { + print "Error in file ".__FILE__." at line ".__LINE__."<br>\n"; + new DB_Error($result->code,PEAR_ERROR_DIE); + } + //set options if (is_array($options)) { @@ -191,6 +203,12 @@ } // end initialize + + // if user made some own tableInfo data, merge them here. + if ($this->user_tableInfo) + { + $tableInfo = $this->array_merge_clobber($tableInfo,$this->user_tableInfo); + } $parent[root] = $this->insertNewResult(&$tableInfo); while ($FirstFetchDone || $res = $result->FetchRow($fetchmode)) @@ -299,7 +317,7 @@ */ function insertNewElement($parent, $res, $key, &$metadata, &$subrow) { - return $parent->new_child($metadata[$key]["name"], utf8_encode($res[$key])); + return $parent->new_child($metadata[$key]["name"], $this->xml_encode($res[$key])); } @@ -313,5 +331,50 @@ function addTableInfo($key, $value, &$metadata) { } + + /** + * + * @param + * @abstract + */ + function xml_encode ($text) { + $text = utf8_encode(ereg_replace("&","&amp;",$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 + * @param + * @abstract + */ + 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; + } + } ?> + +

« previous php.cvs (#4914) next »