Tutorials to .com

Tutorials to .com » Php » Xml » 2,

2,

Print View , by: iSee ,Total views: 8 ,Word Count: 1480 ,Date: Thu, 16 Apr 2009 Time: 12:47 AM

(
$ this-> doXmlString2Xml ($ string, $ xpath);
)

/ **
* Adds an additional pear:: db_result resultset to $ this-> xmldoc
*
* @ Param Object db_result result from a DB-query
* @ See doSql2Xml ()
* @ access public
* /
function addResult ($ result)
(
$ this-> doSql2Xml ($ result);
)

/ **
* Adds an aditional resultset generated from an sql-statement
* To $ this-> xmldoc
*
* @ Param string sql a string containing an sql-statement.
* @ Access public
* @ See doSql2Xml ()
* /
function addSql ($ sql)
(
/ * If there are () expressions in the sql query, we assume it's an xpath expression to
* Be evaluated.
* /

if (preg_match_all ( "/ \ (([^ \)] +) \) / i", $ sql, $ matches))
(
foreach ($ matches [1] as $ match)
(
$ sql = preg_replace ( "# \ (". preg_quote ($ match). "\) #", $ this-> getXpathValue ($ match), $ sql);
)
)
$ result = $ this-> db-> query ($ sql);

/ / very strange
if (PEAR:: isError ($ result-> result)) (
print "You have an SQL-Error: <br>". $ result-> result-> userinfo;
print "<br>";
new DB_Error ($ result-> result-> code, PEAR_ERROR_DIE);
)

$ this-> doSql2Xml ($ result);
)

/ **
* Adds an aditional resultset generated from an Array
* To $ this-> xmldoc
* TODO: more explanation, how arrays are transferred
*
* @ Param array multidimensional array.
* @ Access public
* @ See doArray2Xml ()
* /
function addArray ($ array)
(
$ parent_row = $ this-> insertNewResult (& $ metadata);
$ this-> DoArray2Xml ($ array, $ parent_row);
)

/ **
* Returns an xml-string with a xml-representation of the resultsets.
*
* The resultset can be directly provided here, or if you need more than one
* In your xml, then you have to provide each of them with add () before you
* Call getXML, but the last one can also be provided here.
*
* @ Param mixed $ result result Object from a DB-query
* @ Return string xml
* @ Access public
* /
function getXML ($ result = Null)
(
$ xmldoc = $ this-> getXMLObject ($ result);
return $ xmldoc-> dumpmem ();
)

/ **
* Returns an xml DomDocument Object with a xml-representation of the resultsets. [AutoPage]
*
* The resultset can be directly provided here, or if you need more than one
* In your xml, then you have to provide each of them with add () before you
* Call getXMLObject, but the last one can also be provided here.
*
* @ Param mixed $ result result Object from a DB-query
* @ Return Object DomDocument
* @ Access public
* /
function getXMLObject ($ result = Null)
(
if ($ result) (
$ this-> add ($ result);
)
return $ this-> xmldoc;
)

/ **
* For adding db_result-"trees" to $ this-> xmldoc
* @ Param Object db_result
* @ Access private
* @ See addResult (), addSql ()
* /
function doSql2Xml ($ result)
(

if (DB:: IsError ($ result)) (
print "Error in file". __FILE__. "at line". __LINE__. "<br> \ n";
print $ result-> userinfo. "<br> \ n";
new DB_Error ($ result-> code, PEAR_ERROR_DIE);
)

/ / The method_exists is here, cause tableInfo is only in the cvs at the moment
/ / BE CAREFUL: if you have fields with the same name in different tables, you will get errors
/ / Later, since DB_FETCHMODE_ASSOC doesn't differentiate that stuff.
$ this-> LastResult = & $ result;

if (! method_exists ($ result, "tableInfo") | |! ($ tableInfo = $ result-> tableInfo (False)))
(
/ / emulate tableInfo. this can go away, if every db supports tableInfo
$ fetchmode = DB_FETCHMODE_ASSOC;
$ res = $ result-> FetchRow ($ fetchmode);
$ this-> nested = False;
$ i = 0;

while (list ($ key, $ val) = each ($ res))
(
$ tableInfo [$ i] [ "table"] = $ this-> tagNameResult;
$ tableInfo [$ i] [ "name"] = $ key;
$ resFirstRow [$ i] = $ val;
$ i + +;
)
$ res = $ resFirstRow;
$ FirstFetchDone = True;
$ fetchmode = DB_FETCHMODE_ORDERED;
)
else
(
$ FirstFetchDone = False;
$ fetchmode = DB_FETCHMODE_ORDERED;
)

/ / Initialize db hierarchy ...
$ parenttable = "root";
$ tableInfo [ "parent_key"] [ "root"] = 0;

foreach ($ tableInfo as $ key => $ value) [AutoPage]
(
if (is_int ($ key))
(
/ / If the sql-query had a function the table starts with a # (only in mysql i think ....), then give the field the name of the table before ...
if (preg_match ("/^#/",$ value [ "table"]) | | strlen ($ value [ "table"]) == 0) (
$ value [ "table"] = $ tableInfo [($ key - 1)] [ "table"];
$ tableInfo [$ key] [ "table"] = $ value [ "table"];
)


if (! isset ($ tableInfo [ "parent_table"]) | | is_null ($ tableInfo [ "parent_table"] [$ value] [ "table "]]))
(
$ tableInfo [ "parent_key"] [$ value] [ "table"]] = $ key;
$ tableInfo [ "parent_table"] [$ value] [ "table"]] = $ parenttable;
$ parenttable = $ value [ "table"];
)

)
/ / if you need more tableInfo for later use you can write a function addTableInfo ..
$ this-> addTableInfo ($ key, $ value, & $ tableInfo);
)

/ / 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);

/ / initialize $ resold to get rid of warning messages;
$ resold [0] = "ThisValueIsImpossibleForTheFirstFieldInTheFirstRow";

while ($ FirstFetchDone == True | | $ res = $ result-> FetchRow ($ fetchmode))
(

/ / FirstFetchDone is only for emulating tableInfo, as long as not all dbs support tableInfo. Can go away later
$ FirstFetchDone = False;

while (list ($ key, $ val) = each ($ res))
(

if ($ resold [$ tableInfo] [ "parent_key"] [$ tableInfo] [$ key] [ "table"]]]! = $ res [$ tableInfo] [ "parent_key"] [$ tableInfo] [$ key] [ "table"]]] | |! $ this-> nested)
(
if ($ tableInfo [ "parent_key"] [$ tableInfo] [$ key] [ "table"]] == $ key)
(
if ($ this-> nested | | $ key == 0)
([AutoPage]

$ parent [$ tableInfo] [$ key] [ "table"]] = $ this-> insertNewRow ($ parent [$ tableInfo] [ "parent_table"] [$ tableInfo] [$ key] [ "table "]]], $ res, $ key, & $ tableInfo);
)
else
(
$ parent [$ tableInfo] [$ key] [ "table"]] = $ parent [$ tableInfo] [ "parent_table"] [$ tableInfo] [$ key] [ "table "]]];
)

/ / set all children entries to somethin stupid
foreach ($ tableInfo [ "parent_table"] as $ pkey => $ pvalue)
(
if ($ pvalue == $ tableInfo [$ key] [ "table"])
(
$ resold [$ tableInfo] [ "parent_key"] [$ pkey]] = "ThisIsJustAPlaceHolder";
)
)

)
if ($ parent [$ tableInfo] [$ key] [ "table"]]! = Null)
(
$ this-> insertNewElement ($ parent [$ tableInfo] [$ key] [ "table"]], $ res, $ key, & $ tableInfo, & $ subrow);
)

)
)

$ resold = $ res;
unset ($ subrow);
)
return $ this-> xmldoc;
)



/ **
* For adding whole arrays to $ this-> xmldoc
*
* @ Param array
* @ Param Object domNode
* @ Access private
* @ See addArray ()
* /

function DoArray2Xml ($ array, $ parent) (

while (list ($ key, $ val) = each ($ array))
(
$ tableInfo [$ key] [ "table"] = $ this-> tagNameResult;
$ tableInfo [$ key] [ "name"] = $ key; [AutoPage]
)

if ($ this-> user_tableInfo)
(
$ tableInfo = $ this-> array_merge_clobber ($ tableInfo, $ this-> user_tableInfo);
)
foreach ($ array as $ key => $ value)
(
if (is_array ($ value)) (
if (is_int ($ key))
(
$ valuenew = array_slice ($ value, 0,1);
$ keynew = array_keys ($ valuenew);
$ keynew = $ keynew [0];
)
else
(

$ valuenew = $ value;
$ keynew = $ key;
)

$ rec2 = $ this-> insertNewRow ($ parent, $ valuenew, $ keynew, & $ tableInfo);
$ this-> DoArray2xml ($ value, $ rec2);
)
else (
$ this-> insertNewElement ($ parent, $ array, $ key, & $ tableInfo, & $ subrow);
)
)

)



/ **
* This method sets the options for the class
* One can only set variables, which are defined at the top of
* Of this class.
*
* @ Param array options to be passed to the class
* @ Param boolean if the old suboptions should be deleted
* @ Access public
* @ See $ nested, $ user_options, $ user_tableInfo
* /

function setOptions ($ options, $ delete = False) (
/ / set options
if (is_array ($ options))
(
foreach ($ options as $ option => $ value)
(
if (isset ($ this-> ($ option)))


Php XML Application Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.