电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> PHP
把从SQL中取出的数据转化成XMl格式-PHP教程,XML应用
作者:网友供稿 点击:22
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
使用了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, its 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
    * im 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.
    *  its especially useful, if you dont 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 its 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, its 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, theres
    *           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 its 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 its 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 its 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 its 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()
    */

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·Windows下的PHP5.0安装配制详解-PHP教程,PHP安装
·PHP在XP下IIS和Apache2服务器上的安装-PHP教程,PHP应用
·最近忙于FTP,好站多多!有好多好东东哦!不敢独享!-PHP教程,PHP基础
·PHP 5.0 Pear安装方法-PHP教程,PHP安装
·PHP开发利器-PRADO 1.6(4)-PHP教程,PHP应用
·Sun Sparc Solaris 2.6 Apache-1.3.12+MySQL-3.23.5+PHP-3.0.15 安装记-PHP教程,PHP应用
·php5学习笔记(转)-PHP教程,PHP应用
·APACHE安装笔记-PHP教程,PHP安装
·PHP.MVC的模板标签系统(四)-PHP教程,PHP应用
·PHP.MVC的模板标签系统(二)-PHP教程,PHP应用

最新文章
·PHP源码-利用 QQWry.Dat 实现 IP 地址高效检索
·Php高手带路--问题汇总解答[2]
·PHPQQ编程(2):取QQ在线状态
·php5手动最简安装方法
·福利彩票幸运号码自动生成器
·PHP开发利器-PRADO 1.6
·在Apache 服务器上启用PHP支持
·Windows2000_pro下安装Apache+PHP4+My
·php文件上传的实现
·PHP开发框架的现状和展望




版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号