使用了php的PEAR和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 {
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
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."
\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."
\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}))
{
$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);
}
}
/**
function addXmlFile($file,$xpath = Null)
{
$fd = fopen( $file, "r" );
$content = fread( $fd, filesize( $file ) );
fclose( $fd );
$this->doXmlString2Xml($content,$xpath);
}
/**