Location: PHPKode > scripts > MsSql To XML > mssql-to-xml/sqlToXml.inc.php
<?
require_once("mssql.inc.php");

class sqlToXml{
	private $query="";
	private $version="";
	private $rootNodeName="root";
	private $xml;
	private $xmlString;
	private $sql;
	
	public function __construct($myQuery,$version="1.0"){
		$this->setQuery($myQuery);
		$this->setVersion($version);
		$this->sql = new MsSQL();
	}
	public function setRootNodeName($name){
		$this->rootNodeName=$name;
	}
	private function setVersion($myVersion){
		$this->version=$myVersion;
	}
	private function setQuery($myQuery){
		$this->query=$myQuery;
	}
	public function createXml(){
		$this->xml = new DomDocument($this->version);//object referance for the new document 
		//Adding our root node
		$root = $this->xml->createElement($this->rootNodeName);
		$root = $this->xml->appendChild($root);
		//Querying the db
		$sqlResult=$this->sql->query($this->query);
		
   		while ($object = mssql_fetch_object($sqlResult)){
   			//Add a node for each row
   			$nodeForEachRow = $this->xml->createElement("row");
  			$nodeForEachRow = $root->appendChild($nodeForEachRow);

   			foreach($object as $key => $fieldvalue) {
				//Add node for each column
   				$child = $this->xml->createElement($key);
    			$child = $nodeForEachRow->appendChild($child);
    			//Add values as a text node (inserting values!)
    			$value = $this->xml->createTextNode($fieldvalue);
    			$value = $child->appendChild($value);
   			}
   		}
   		$this->xmlString=$this->xml->saveXML();//returns the completed XML document as a string.
   	return $this->xmlString;
	}
	public function __destruct(){
		$this->query="";
		$this->version="";
		unset($this->sql);
	}
}
?>
Return current item: MsSql To XML