首页 > 作文

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

更新时间:2023-04-07 04:43:29 阅读: 评论:0

使用了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 licen果树盆栽,

|// | that is bundled with this package in the file licen, and is

|// | available at through the world-wide-web at

|// | /d/file/titlepic/manual-lookup.php />
|// | if you did not receive a copy of the php licen and are unable to

|// | obtain it through the world-wide-web, plea nd a note to

|// | licen@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-reprentation 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->tencoding(“iso-8859-1″,”utf-8”);

* $result = $db->query(“lect * 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(“lect * from bands”);

* $xmlstring = $sql2xml->getxml();

*

* more documentation and a tutorial/how-to can be found at

*http://php.chregu.tv/sql2xml

*

* @authorchristian stocker <chregu@bitflux.ch>

* @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 (e ur_tableinfo)

*

* @varboolean

* @e$ur_tableinfo, dosql2xml(), doarray2xml();

*/

var $nested = true;

/**

* name of the tag element for resultts

*

* @varstring

* @einrtnewresult()

*/

var $tagnameresult = “result”;

/**

* name of the tag element for rows

*

* @varstring

* @einrtnewrow()

*/

var $tagnamerow = “row”;

/**

*

* @varobject pear::db

* @access private

*/

var $db = null;

/**

* options to be ud in extended class (for example in sql2xml_ext).

* they are pasd with toptions as an array (arrary(“ur_options” = array());

*and can then be accesd with $this->ur_options[“bla”] from your

*extended class for additional features.

*this array is not u in this ba class, it’s only for passing easy parameters

*to extended class.

*

* @vararray

*/

var $ur_options = array();

/**

* the domdocument object to be ud 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($this->xmldoc);

*

* @varobject domnode

* @accessprivate

*/

var $xmlroot;

/**

* this array is ud to give the structure of your databa to the class.

*it’s especially uful, if you don’t u mysql, since other rdbms than

*mysql are not able at the moment to provide the right information about

*your databa 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/co光合作用的化学方程式mples queries and with arrays, it’s impossible

*to find out to which table the field actually belon湖北旅游景点gs. 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

*u for that, e 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 “rowt” in the

*parent 剑网3升级table.

*

*$tableinfo[“parent_table”][$table]: name of the parent table for $table.

*

* @vararray

* @accessprivate

*/

var $ur_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://ur: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-resultt or as an array. providing sql-strings will

*not work.

* the $root param is ud, 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 resultt/array/sql-string.

*and the first tag of this result is ud 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 (is_string($dsn))

{

include_once (“db.php”);

$this->db = db::connect($dsn);

if (db::irror($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);

}

}

elif (is_object($dsn) && db::irror($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

elif (get_parent_class($dsn) == “db_common”)

{

$this->db = $dsn;

}

$this->xmldoc = domxml_new_xmldoc(‘1.0’);

//oehm, ems 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 (!ist($this->xmlroot->{$this->tagname}))

{

$this->tagname = “name”;

}

}

}

/**

* general method for adding new resultts 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

* @eaddresult(), addsql(), addarray(), addxmlfile()

*/

function add ($resultt, $params = null)

{

// if string, then it’s a query, a xml-file or a xml-string…

if (is_string($resultt)) {

if (preg_match(“/\.xml$/”,$resultt)) {

$this->addxmlfile($resultt,$params);

}

elif (preg_match(“/.*lect.*from.*/i” ,$resultt)) {

$this->addsql($resultt);

}

el {

$this->addxmlstring($resultt);

}

}

// if array, then it’s an array…

elif (is_array($resultt)) {

$this->addarray($resultt);

}

if (get_class($resultt) == “db_result”) {

$this->addresult($resultt);

}

}

/**

* adds the content of a xml-file to $this->xmldoc, on the same level

* as a normal resultt (mostly just below <root>)

*

* @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 inrted before the result

* @accesspublic

* @edoxmlstring2xml()

*/

function addxmlfile($file,$xpath = null)

{

$fd = fopen( $file, “r” );

$content = fread( $fd, filesize( $file ) );

fclo( $fd );

$this->doxmlstring2xml($content,$xpath);

}

/**

* adds the content of a xml-string to $this->xmldoc, on the same level

* as a normal resultt (mostly just below <root>)

*

* @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 inrted before the result

* @accesspublic

* @edoxmlstring2xml()

*/

本文发布于:2023-04-07 04:43:23,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/0e2bdd9826e827734c027cefe6c883da.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:把从SQL中取出的数据转化成XMl格式.doc

本文 PDF 下载地址:把从SQL中取出的数据转化成XMl格式.pdf

相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图