Tutorials to .com

Tutorials to .com » Php » Xml » Removed from the SQL data into XMl format

Removed from the SQL data into XMl format

Print View , by: iSee ,Total views: 4 ,Word Count: 1953 ,Date: Thu, 16 Apr 2009 Time: 1:21 AM

The use of php's PEAR and DB
<? php
/ / +----------------------------------------------- -----------------------+
/ / | PHP version 4.0 |
/ / +----------------------------------------------- -----------------------+
/ / | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
/ / +----------------------------------------------- -----------------------+
/ / | This source file is subject to version 2.0 of the PHP license, |
/ / | That is bundled with this package in the file LICENSE, and is |
/ / | Available at through the world-wide-web at |
/ / | Http://www.php.net/license/2_02.txt. |
/ / | If you did not receive a copy of the PHP license and are unable to |
/ / | Obtain it through the world-wide-web, please send a note to |
/ / | License@php.net so we can mail you a copy immediately. |
/ / +----------------------------------------------- -----------------------+
/ / | Authors: Christian Stocker <chregu@phant.ch> |
/ / +----------------------------------------------- -----------------------+
/ /
/ / $ Id: sql2xml.php, v 1.59 2001/11/13 10:54:02 chregu Exp $

/ **
* This class takes a PEAR:: DB-Result Object, a sql-query-string or an array
* And returns a xml-representation of it.
*
* TODO
*-Encoding etc, options for header
*-ERROR CHECKING
*
* Usage example
*
* Include_once ( "DB.php");
* Include_once ( "XML/sql2xml.php");
* $ Db = DB:: connect ( "mysql: / / root @ localhost / xmltest");
* $ Sql2xml = new xml_sql2xml ();
* / / The next one is only needed, if you need others than the default
* $ Sql2xml-> setEncoding ( "ISO-8859-1", "UTF-8");
* $ Result = $ db-> query ( "select * from bands");
* $ Xmlstring = $ sql2xml-> getXML ($ result);
*
* Or
*
* Include_once ( "DB.php");
* Include_once ( "XML/sql2xml.php");
* $ Sql2xml = new xml_sql2xml ( "mysql: / / root @ localhost / xmltest");
* $ Sql2xml-> Add ( "select * from bands");
* $ Xmlstring = $ sql2xml-> getXML ();
*
* More documentation and a tutorial / how-to can be found at
* Http://php.chregu.tv/sql2xml
*
* @ Author Christian Stocker <chregu@bitflux.ch>
* @ Version $ Id: sql2xml.php, v 1.59 2001/11/13 10:54:02 chregu Exp $
* @ Package xml
* /
class XML_sql2xml (

/ **
* If joined-tables should be output nested.
* Means, if you have joined two or more queries, the later
* Specified tables will be nested within the result of the former
* Table.
* Works at the moment only with mysql automagically. For other RDBMS
* You have to provide your table-relations by hand (see user_tableinfo)
*
* @ Var boolean
* @ See $ user_tableinfo, doSql2Xml (), doArray2Xml ();
* /
var $ nested = True;

/ **
* Name of the tag element for resultsets
*
* @ Var string
* @ See insertNewResult ()
* /
var $ tagNameResult = "result";

/ **
* Name of the tag element for rows
*
* @ Var string
* @ See insertNewRow ()
* /
var $ tagNameRow = "row";

/ **
*
* @ Var object PEAR:: DB
* @ access private [AutoPage]
* /
var $ db = Null;


/ **
* Options to be used in extended Classes (for example in sql2xml_ext).
* They are passed with SetOptions as an array (arrary ( "user_options" = array ());
* And can then be accessed with $ this-> user_options [ "bla"] from your
* Extended classes for additional features.
* This array is not use in this base class, it's only for passing easy parameters
* To extended classes.
*
* @ Var array
* /
var $ user_options = array ();


/ **
* The DomDocument Object to be used in the whole class
*
* @ Var object DomDocument
* @ Access private
* /
var $ xmldoc;


/ **
* The Root of the domxml object
* I'm not sure, if we need this as a class variable ....
* Could be replaced by domxml_root ($ this-> xmldoc);
*
* @ Var object DomNode
* @ Access private
* /
var $ xmlroot;


/ **
* This array is used to give the structure of your database to the class.
* It's especially useful, if you don't use mysql, since other RDBMS than
* Mysql are not able at the moment to provide the right information about
* Your database structure within the query. And if you have more than 2
* Tables joined in the sql it's also not possible for mysql to find out
* Your real relations.
* The parameters are the same as in fieldInfo from the PEAR:: DB and some
* Additional ones. Here they come:
* From PEAR:: DB-> fieldinfo:
*
* $ TableInfo [$ i] [ "table"]: the table, which field # $ i belongs to.
* For some rdbms / comples queries and with arrays, it's impossible
* To find out to which table the field actually belongs. You can
* Specify it here more accurate. Or if you want, that one fields
* Belongs to another table, than it actually says (yes, there's
* Use for that, see the upcoming tutorial ...)
*
* $ TableInfo [$ i] [ "name"]: the name of field # $ i. if you want another
* Name for the tag, than the query or your array provides, assign
* It here.
*
* Additional info
* $ TableInfo [ "parent_key"] [$ table]: index of the parent key for $ table.
* This is the field, where the programm looks for changes, if this
* Field changes, it assumes, that we need a new "rowset" in the
* Parent table.
*
* $ TableInfo [ "parent_table"] [$ table]: name of the parent table for $ table.
*
* @ Var array
* @ Access private
* /
var $ user_tableInfo = array ();

/ ** [AutoPage]
* The encoding type, the input from the db has
* /
var $ encoding_from = "ISO-8859-1";

/ **
* The encoding type, the output in the xml should have
* (Note that domxml at the moment only support UTF-8, or at least it looks like)
* /
var $ encoding_to = "gb2312";

var $ tagname = "tagname";

/ **
* Constructor
* The Constructor can take a Pear:: DB "data source name" (eg.
* "Mysql: / / user: passwd @ localhost / dbname") and will then connect
* To the DB, or a PEAR:: DB object link, if you already connected
* The db before.
"If you provide nothing as $ dsn, you only can later add stuff with
* A pear:: db-resultset or as an array. Providing sql-strings will
* Not work.
* The $ root param is used, if you want to provide another name for your
* Root-tag than "root". If you give an empty string (""), there will be no
* Root element created here, but only when you add a resultset / array / sql-string.
* And the first tag of this result is used as the root tag.
*
* @ Param mixed $ dsn PEAR:: DB "data source name" or object DB object
* @ Param string $ root the name of the xml-doc root element.
* @ Access public
* /
function XML_sql2xml ($ dsn = Null, $ root = "root") (

/ / If it's a string, then it must be a dsn-identifier;

if (is_string ($ dsn))
(
include_once ( "DB.php");
$ this-> db = DB:: Connect ($ dsn);
if (DB:: isError ($ this-> db))
(
print "The given dsn for XML_sql2xml was not valid in file". __FILE__. "at line". __LINE__. "<br> \ n";
return new DB_Error ($ this-> db-> code, PEAR_ERROR_DIE);
)

)

elseif (is_object ($ dsn) & & DB:: isError ($ dsn))
(
print "The given param for XML_sql2xml was not valid in file". __FILE__. "at line". __LINE__. "<br> \ n";
return new DB_Error ($ dsn-> code, PEAR_ERROR_DIE);
)

/ / If parent class is db_common, then it's already a connected identifier
elseif (get_parent_class ($ dsn) == "db_common")
(
$ this-> db = $ dsn;
)

$ this-> xmldoc = domxml_new_xmldoc ('1 .0 ');

/ / oehm, seems not to work, unfortunately .... does anybody know a solution?
$ this-> xmldoc-> encoding = $ this-> encoding_to;

if ($ root) (
$ this-> xmlroot = $ this-> xmldoc-> add_root ($ root);
/ / PHP 4.0.6 had $ root-> name as tagname, check for that here ...
if (! isset ($ this-> xmlroot-> ($ this-> tagname))) [AutoPage]
(
$ this-> tagname = "name";
)
)

)

/ **
* General method for adding new resultsets to the xml-object
* Give a sql-query-string, a pear:: db_result object or an array as
* Input parameter, and the method calls the appropriate method for this
* Input and adds this to $ this-> xmldoc
*
* @ Param string sql-string, or object db_result, or array
* @ Param mixed additional parameters for the following functions
* @ Access public
* @ See addResult (), addSql (), addArray (), addXmlFile ()
* /
function add ($ resultset, $ params = Null)
(

/ / If string, then it's a query, a xml-file or a xml-string ...
if (is_string ($ resultset)) (
if (preg_match ( "/ \. xml $/",$ resultset)) (
$ this-> AddXmlFile ($ resultset, $ params);
)
elseif (preg_match ("/.* select .* from .* / i ", $ resultset)) (
$ this-> AddSql ($ resultset);
)
else (
$ this-> AddXmlString ($ resultset);
)

)
/ / If array, then it's an array ...
elseif (is_array ($ resultset)) (
$ this-> AddArray ($ resultset);
)

if (get_class ($ resultset) == "db_result") (
$ this-> AddResult ($ resultset);
)
)

/ **
* Adds the content of a xml-file to $ this-> xmldoc, on the same level
* As a normal resultset (mostly just below <root>)
*
* @ Param string filename
* @ 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 public
* @ See doXmlString2Xml ()
* /

function addXmlFile ($ file, $ xpath = Null)
(
$ fd = fopen ($ file, "r");
$ content = fread ($ fd, filesize ($ file));
fclose ($ fd);
$ this-> doXmlString2Xml ($ content, $ xpath);
)

/ **
* Adds the content of a xml-string to $ this-> xmldoc, on the same level
* As a normal resultset (mostly just below <root>)
*
* @ Param string xml
* @ 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 public
* @ See doXmlString2Xml () [AutoPage]
* /


Php XML Application Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.