PX : code

MySQL2XML by Adi Sieker
Download this code


<?php
/*
Copyright (C) 2002 by Adi Sieker  All rights reserved.
contact: adi@sieker.info
*/
/*
This is a sample script. Paste this in to
an empty php3 file and run it.

require_once "mysql2xml.inc.php";

   $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();
*/
require_once "DB.php";

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 != )
     {
      
$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->DSN "mysql://$Usr:$Pwd@$DBHost/$DBName";
     
$this->SQLDataSource     $SQLTable;
     
$this->XMLRootName       $XMLRoot;
     
$this->XMLRecordNodeName $XMLRecord;
     
$this->Encoding          $Enc;
     
$this->iNumNodes 0;
  }

  function 
AddNode$SQLField$XMLNode$bCDATA 0$Char )
  {
     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-)
            
$SQLString $SQLString ",";
         
$SQLString $SQLString " ";

         if( 
strlen($CurNode->Operator) > &&
               
strlen($CurNode->Value) > )
         {
            
$WhereString $WhereString $CurNode->SQLFieldName .
                           
" " $CurNode->Operator " " $CurNode->CharField .
                           
$CurNode->Value $CurNode->CharField" AND ";
         }
      }

      
$SQLString $SQLString "FROM " $this->SQLDataSource;

      if( 
substr($WhereStringstrlen($WhereString)-5,5) ==" AND " )
      {
         
$WhereString substr($WhereString0,strlen($WhereString)-5);
         
$SQLString $SQLString " " $WhereString;
      }



      
$this->SQLString $SQLString;
     if( 
$__debug__ == 1)
      echo 
"<br>" $this->SQLString;
  }

  function 
EchoXML()
  {
     global 
$__debug__;
      if( 
strlen($this->SQLString) == )
         
$this->GenerateSQL();

      
// open connection to mysql
      
$db DB::connect($this->DSN);
      if(
DB::isError($db) ) {
         echo(
"error connecting to database reason:" $db->getMessage());
         return;
      }

      
$result $db->query($this->SQLString);
      if(
DB::isError($result)) {
         
decho("error querying database reason:" $result->getMessage());
         
err_write("failed to get credentials");
         return;
      }

      echo 
"<?xml version=\"1.0\" encoding=\"" $this->Encoding "\"?"">";
      echo 
"<" $this->XMLRootName ">";
      while( 
$row $result->fetchrow(DB_FETCHMODE_ASSOC))
      {
         echo 
"<" $this->XMLRecordNodeName ">";
         for(
$i=0$i $this->iNumNodes$i++)
         {
            
$CurNode $this->NodeList[$i];

            echo 
"<" $CurNode->XMLNodeName ">" $CurNode->CDATANodeBeg .
                  
$row[$CurNode->SQLFieldName] .
                  
$CurNode->CDATANodeEnd "</" $CurNode->XMLNodeName ">";
         }
         echo 
"</" $this->XMLRecordNodeName ">";

      }
      echo 
"</" $this->XMLRootName ">";
  }

  function 
GetXML()
  {
     global 
$__debug__;
      if( 
strlen($this->SQLString) == )
         
$this->GenerateSQL();

      
// open connection to mysql
      
$db DB::connect($this->DSN);
      if(
DB::isError($db) ) {
         echo(
"error connecting to database reason:" $db->getMessage());
         return;
      }

      
$result $db->query($this->SQLString);
      if(
DB::isError($result)) {
         
decho("error querying database reason:" $result->getMessage());
         
err_write("failed to get credentials");
         return;
      }

      
$ret "<?xml version=\"1.0\" encoding=\""  $this->Encoding "\"?"">";
      
$ret $ret "<" $this->XMLRootName ">";
      while( 
$row $result->fetchrow(DB_FETCHMODE_ASSOC))
      {
         
$ret .= "<" $this->XMLRecordNodeName ">";
         for(
$i=0$i $this->iNumNodes$i++)
         {
            
$CurNode $this->NodeList[$i];

            
$ret .= "<" $CurNode->XMLNodeName ">" $CurNode->CDATANodeBeg .
                  
$row[$CurNode->SQLFieldName] .
                  
$CurNode->CDATANodeEnd "</" $CurNode->XMLNodeName ">";
         }
        
$ret .= "</" $this->XMLRecordNodeName ">";

      }
      
$ret .= "</" $this->XMLRootName ">";

      return 
$ret;
  }
}
?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.