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

时间:2021-05-18


使用了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://mon")
{
$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
*
* @paramstring sql-string, or object db_result, or array
* @parammixed additional parameters for the following functions
* @accesspublic
* @seeaddResult(), 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>)
*
* @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($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>)
*
* @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()
*/

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章