欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

把从SQL中取出的数据转化成XMl格式

程序员文章站 2022-05-16 12:55:03
使用了php的pear和db

使用了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 {

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

    /**
    * 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}))
            {
                $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()
    */