使用了php的PEAR和DB|//
+———————————————————————-+////
$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
;* include_once;* $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;* $result =
$db->query(“select * from bands”);* $xmlstring =
$sql2xml->getXML;** or** include_once ;* include_once;*
$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* @authorChristian Stocker
* @version$Id: sql2xml.php,v 1.59 2001/11/13 10:54:02 chregu Exp $*
@packageXML*/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 **
@varboolean* @see$user_tableinfo, doSql2Xml;*/var $nested =
True;/*** Name of the tag element for resultsets** @varstring*
@seeinsertNewResult()*/var $tagNameResult = “result”;/*** Name of
the tag element for rows** @varstring* @seeinsertNewRow()*/var
$tagNameRow = “row”;/**** @varobject PEAR::DB* @access
private*/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.**
@vararray*/var $user_options = array();/*** The DomDocument Object
to be used in the whole class** @varobjectDomDocument*
@accessprivate*/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;** @varobject DomNode* @accessprivate*/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.** @vararray* @accessprivate*/var
$user_tableInfo = array();/*** 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.** @parammixed $dsnPEAR::DB “data source name” or object
DB object* @paramstring $rootthe name of the xml-doc root element.*
@accesspublic*/function XML_sql2xml ($dsn = Null, $root = “root”) {//
if it’s a string, then it must be a dsn-identifier;if {include_once
;$this->db = DB::Connect;if (DB::isError{print “The given dsn for
XML_sql2xml was not valid in file “.__FILE__.” at line
“.__LINE__.”
n”;return new
DB_Error($this->db->code,PEAR_ERROR_DIE);}}elseif &&
DB::isError{print “The given param for XML_sql2xml was not valid in
file “.__FILE__.” at line “.__LINE__.”
n”;return new DB_Error($dsn->code,PEAR_ERROR_DIE);}// if parent
class is db_common, then it’s already a connected identifierelseif
(get_parent_class{$this->db = $dsn;}$this->xmldoc =
domxml_new_xmldoc;//oehm, seems not to work, unfortunately…. does
anybody know a solution?$this->xmldoc->encoding =
$this->encoding_to;if {$this->xmlroot =
$this->xmldoc->add_root;//PHP 4.0.6 had $root->name as
tagname, check for that here…if
(!isset($this->xmlroot->{$this->tagname})){$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** @paramstring
sql-string, or object db_result, or array* @parammixed additional
parameters for the following functions* @accesspublic* @seeaddResult,
addArray*/function add ($resultset, $params = Null){// if string, then
it’s a query, a xml-file or a xml-string…if ) {if
(preg_match(“/.xml$/”,$resultset)) {$this->AddXmlFile;}elseif
(preg_match(“/.*select.*from.*/i” ,$resultset))
{$this->AddSql;}else {$this->AddXmlString;}}// if array, then it’s
an array…elseif {$this->AddArray;}if == “db_result”)
{$this->AddResult;}}/*** Adds the content of a xml-file to
$this->xmldoc, on the same level* as a normal resultset (mostly just
below )** @paramstring filename* @parammixed xpatheither a string
with the xpath expression or an array with “xpath”=>xpath
expressionand “root”=tag/subtag/etc, which are the tags to be inserted
before the result* @accesspublic* @seedoXmlString2Xml()*/function
addXmlFile{$fd = fopen;$content = fread( $fd,
filesize;fclose;$this->doXmlString2Xml;}/*** Adds the content of a
xml-string to $this->xmldoc, on the same level* as a normal
resultset (mostly just below )** @paramstring xml* @parammixed
xpatheither a string with the xpath expression or an array with
“xpath”=>xpath expressionand “root”=tag/subtag/etc, which are the
tags to be inserted before the result* @accesspublic*
@seedoXmlString2Xml()*/

发表评论

电子邮件地址不会被公开。 必填项已用*标注