Convert data taken from SQL to XML format _php tutorial

Source: Internet
Author: User
Tags tagname xpath
PHP pear and DB are used
// +----------------------------------------------------------------------+
// | PHP version 4.0
|// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2001 the PHP Group
|// +----------------------------------------------------------------------+
// | This source file was subject to version 2.0 of the PHP license,
|// | That's bundled with the "This" file LICENSE, and is
|// | Available at through the World-wide-web at
|// | Http://www.php.net/license/2_02.txt.
|// | If you do not receive a copy of the PHP license and is 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
|// +----------------------------------------------------------------------+
//
$Id: sql2xml.php,v 1.59 2001/11/13 10:54:02 Chregu EXP $

/**
* This class takes a PEAR::D b-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");
* $sql 2xml = new Xml_sql2xml ();
*//the next one is only needed if you need others than the default
* $sql 2xml->setencoding ("Iso-8859-1", "UTF-8");
* $result = $db->query ("SELECT * from bands");
* $xmlstring = $sql 2xml->getxml ($result);
*
* OR
*
* Include_once ("db.php");
* Include_once ("xml/sql2xml.php");
* $sql 2xml = new Xml_sql2xml ("Mysql://root@localhost/xmltest");
* $sql 2xml->add ("SELECT * from bands");
* $xmlstring = $sql 2xml->getxml ();
*
* More documentation and a tutorial/how-to can is found at
* Http://php.chregu.tv/sql2xml
*
* @author Christian Stocker
* @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 and more queries, the later
* Specified tables'll be nested within the result of the former
* table.
* Works at the moment only with MySQL automagically. For-Other RDBMS
* 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::D b
* @access Private
*/
var $db = Null;


/**
* Options to being used in extended Classes (for example in Sql2xml_ext).
* They is passed with SetOptions as an array (Arrary ("user_options" = Array ());
* and can then is accessed with $this->user_options["Bla") from your
* Extended classes for additional features.
* This array was not the use of this base class, it's only for passing easy parameters
* to extended classes.
*
* @var Array
*/
var $user _options = Array ();


/**
* The DomDocument Object to being 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 is replaced by Domxml_root ($this->xmldoc);
*
* @var Object Domnode
* @access Private
*/
var $xmlroot;


/**
* This array was used to give the structure of the your database to the class.
* It ' s especially useful, if you don't use MySQL, since other RDBMS than
* MySQL is not able on 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 is the same as in FieldInfo from the PEAR::D B and some
* Additional ones. Here they Come:
* FROM PEAR::D b->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 more accurate. Or if you want, which one fields
* Belongs to another table, than it actually says (yes, there ' s
* Use for this, 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 has
* (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::D B "Data source name" (eg.
* "Mysql://user:passwd@localhost/dbname") and would then connect
* to the DB, or a PEAR::D B-Object link, if you already connected
* The DB before.
"If you provide no as $DSN, you can later add stuff with
* A pear::d b-resultset or as an array. Providing sql-strings would
* 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'll be no
* Root element created here, and only if you add a resultset/array/sql-string.
* and the first tag of this result is used as the root tag.
*
* @param mixed $dsn PEAR::D B "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 is 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 is not valid in file". __file__. ". __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 is not a valid in file". __file__. "On 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 ...
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::d B_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, at the same level
* As a normal resultset (mostly just below )
*
* @param string filename
* @param mixed XPath either a string with the XPath expression or an array with "XPath" =>xpath expression and "root" =ta G/SUBTAG/ETC, which is 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, at the same level
* As a normal resultset (mostly just below )
*
* @param string XML
* @param mixed XPath either a string with the XPath expression or an arr Ay with "XPath" =>xpath expression and "root" =tag/subtag/etc, which is the tags to be inserted before the result
* @access Public
* @see doxmlstring2xml ()
*/

ht tp://www.bkjia.com/phpjc/313946.html www.bkjia.com true http:// www.bkjia.com/PHPjc/313946.html techarticle using PHP pear and db? PHP//+----------------------------------------------------------------- -----+ // | PHP version 4.0 |//+------------------------------------------------...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.