|
|
|
<?php
/*
This is a sample script. Paste this in to
an empty php3 file and run it.
if( empty($xml_inc_php3) )
include("xml.inc.php3");
$XMLGenerator = new XMLDefinition
( "hostname", "databasename", "username", "password", "tablename", "rootnodename", "rownodename","iso-8859-1");
$XMLGenerator->AddNode( "id", "recordid" );
$XMLGenerator->AddNode( "prj_id", "projectid" );
$XMLGenerator->AddNode( "subprj_id", "subprjid" );
$XMLGenerator->AddNode( "taet_id", "whatid" );
$XMLGenerator->AddNode( "mitarb_id", "programerid" );
$XMLGenerator->AddNode( "Bemerkung", "comment", 1 , 1);
$XMLGenerator->AddNode( "Wert", "duration" );
$XMLGenerator->AddNode( "Datum", "date" );
$XMLGenerator->AddRestriction("projectid", "=", "57");
echo $XMLGenerator->GetXML();
//$XMLGenerator->EchoXML();
*/
/*
This class REQUIRES the mysql Database Class by me available
from http://www.weberdev.com/get_example.php3?count=1505.
You can contact me by mail at adi@living-source.com.
*/
if( empty($database_inc_php3) )
include("database.inc.php3");
class NodeDefinition
{
var $SQLFieldName;
var $XMLNodeName;
var $CDATANodeBeg;
var $CDATANodeEnd;
var $Operator;
var $Value;
var $CharField;
function NodeDefinition($SQLField, $XMLNode, $bCDATA = 0, $Char = 0)
{
global $__debug__;
$this->SQLFieldName = $SQLField;
$this->XMLNodeName = $XMLNode;
if( $bCDATA != 0 )
{
$this->CDATANodeBeg = "<![CDATA[";
$this->CDATANodeEnd = "]]>";
}
else
{
$this->CDATANodeBeg = "";
$this->CDATANodeEnd = "";
}
if( $Char != 0)
$this->CharField = "'";
}
function AddRestriction( $Op, $Val)
{
global $__debug__;
$this->Operator = $Op;
$this->Value = $Val;
}
}
class XMLDefinition
{
var $UserName;
var $Password;
var $DataBaseHost;
var $DataBaseName;
var $SQLDataSource;
var $XMLRootName;
var $XMLRecordNodeName;
var $NodeList;
var $iNumNodes;
var $SQLString;
var $Encoding;
function XMLDefinition( $DBHost, $DBName, $Usr, $Pwd, $SQLTable, $XMLRoot, $XMLRecord = "record", $Enc = "iso-8859-1")
{
global $__debug__;
$this->DataBaseHost = $DBHost;
$this->DataBaseName = $DBName;
$this->UserName = $Usr;
$this->Password = $Pwd;
$this->SQLDataSource = $SQLTable;
$this->XMLRootName = $XMLRoot;
$this->XMLRecordNodeName = $XMLRecord;
$this->Encoding = $Enc;
$this->iNumNodes = 0;
}
function AddNode( $SQLField, $XMLNode, $bCDATA = 0, $Char = 0 )
{
global $__debug__;
$this->NodeList[$this->iNumNodes] = new NodeDefinition($SQLField, $XMLNode, $bCDATA, $Char);
$this->iNumNodes++;
}
function AddRestriction( $XMLNode, $Oper, $Valu )
{
global $__debug__;
$CurNode = false;
for($i=0; $i < $this->iNumNodes; $i++)
{
$CurNode = $this->NodeList[$i];
if( $CurNode->XMLNodeName == $XMLNode )
{
if( $CurNode )
{
$CurNode->AddRestriction( $Oper, $Valu);
$this->NodeList[$i] = $CurNode;
}
break;
}
}
}
function GenerateSQL()
{
global $__debug__;
$this->SQLString = "";
$SQLString = "SELECT ";
$WhereString = " WHERE ";
for($i=0; $i < $this->iNumNodes; $i++)
{
$CurNode = $this->NodeList[$i];
$SQLString = $SQLString . $CurNode->SQLFieldName;
if( $i < $this->iNumNodes-1 )
$SQLString = $SQLString . ",";
$SQLString = $SQLString . " ";
if( strlen($CurNode->Operator) > 0 &&
strlen($CurNode->Value) > 0 )
{
$WhereString = $WhereString . $CurNode->SQLFieldName .
" " . $CurNode->Operator . " " . $CurNode->CharField .
$CurNode->Value . $CurNode->CharField. " AND ";
}
}
$SQLString = $SQLString . "FROM " . $this->SQLDataSource;
if( substr($WhereString, strlen($WhereString)-5,5) ==" AND " )
{
$WhereString = substr($WhereString, 0,strlen($WhereString)-5);
$SQLString = $SQLString . " " . $WhereString;
}
$this->SQLString = $SQLString;
if( $__debug__ == 1)
echo "<br>" . $this->SQLString;
}
function EchoXML()
{
global $__debug__;
if( strlen($this->SQLString) == 0 )
$this->GenerateSQL();
$opDataBase = new MySQLDatabase;
$opDataBase->Open( $this->DataBaseName, $this->DataBaseHost, $this->UserName, $this->Password );
$opDataBase->Query($this->SQLString);
echo "<?xml version=\"1.0\" encoding=\"" . $this->Encoding . "\"?>";
echo "<" . $this->XMLRootName . ">";
while( $opDataBase->NextRow())
{
echo "<" . $this->XMLRecordNodeName . ">";
for($i=0; $i < $this->iNumNodes; $i++)
{
$CurNode = $this->NodeList[$i];
echo "<" . $CurNode->XMLNodeName . ">" . $CurNode->CDATANodeBeg .
$opDataBase->GetField( $CurNode->SQLFieldName) .
$CurNode->CDATANodeEnd . "</" . $CurNode->XMLNodeName . ">";
}
echo "</" . $this->XMLRecordNodeName . ">";
}
echo "</" . $this->XMLRootName . ">";
}
function GetXML()
{
global $__debug__;
if( strlen($this->SQLString) == 0 )
$this->GenerateSQL();
$opDataBase = new MySQLDatabase;
$opDataBase->Open( $this->DataBaseName, $this->DataBaseHost, $this->UserName, $this->Password );
$opDataBase->Query($this->SQLString);
$ret = "<?xml version=\"1.0\" encoding=\"" . $this->Encoding . "\"?>";
$ret = $ret . "<" . $this->XMLRootName . ">";
while( $opDataBase->NextRow())
{
$ret .= "<" . $this->XMLRecordNodeName . ">";
for($i=0; $i < $this->iNumNodes; $i++)
{
$CurNode = $this->NodeList[$i];
$ret .= "<" . $CurNode->XMLNodeName . ">" . $CurNode->CDATANodeBeg .
$opDataBase->GetField( $CurNode->SQLFieldName) .
$CurNode->CDATANodeEnd . "</" . $CurNode->XMLNodeName . ">";
}
$ret .= "</" . $this->XMLRecordNodeName . ">";
}
$ret .= "</" . $this->XMLRootName . ">";
return $ret;
}
}
$xml_inc_php3 = 1;
?>
|
|
| Glossword - glossary compiler Categories : Content Management, PHP, MySQL, XML | | | MySQL or SQL Query to XML Output Categories : PHP, MySQL, XML, Databases | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | DBXML- A Class to backup databases in XML Format using web interface Categories : PHP, PHP Classes, Databases, MySQL, XML | | | TAB_STRUCT Class: Is supporting Class for the DBXML Class Categories : PHP, PHP Classes, MySQL, XML, Databases | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | Cut your MySQL Connections to 1 line of code Categories : PHP, Beginner Guides, Databases, MySQL | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Displaying records of database in more than one page (paging) Categories : Databases, MySQL, PHP | | | Retrieve text from table and email to your e-
address in pipe delimited format. Categories : PHP, MySQL | | | phpCards - PHP/mySQL postcard script with web based admin to add, edit, and delete cards and categories. Very easy to install. Categories : PHP, Complete Programs, HTML and PHP, MySQL | | | PCAC (pretty cool auction client), auction client server applications Categories : PHP, MySQL, Ecommerce | | | html split bar used to split in multiple pages a database result Categories : HTML and PHP, Databases, MySQL, PHP | | | Tropicalm Genetree Family (MySQL based family tree) Categories : PHP, Interfaces, Databases, MySQL, Complete Programs | |
| | | | Bjorn Hrobjartsson wrote :644
Thnx for this, very nice coding!
I would very much appreciate some documentation or more examples with the code, f.e. does the class support direct SQL queries to the db or how to add attributes / Cdata to a node etc.
Anyways, very nice work and thnx again.
Regards,
Bjorn Hrobjartsson
webmaster@online.is
| |
|
|
|