.
*/
require_once 'phing/system/io/PhingFile.php';
require_once 'phing/tasks/ext/pdo/PDOResultFormatter.php';
/**
* XML formatter for PDO results.
*
* This class reprsents the output of a query using a simple XML schema.
*
*
*
* value
* value2
*
*
* value
* value2
*
*
*
* The actual names of the colums will depend on the fetchmode that was used
* with PDO.
*
* @author Hans Lellelid
* @package phing.tasks.ext.pdo
* @since 2.3.0
*/
class XMLPDOResultFormatter extends PDOResultFormatter {
/**
* The XML document being created.
* @var DOMDocument
*/
private $doc;
/**
* @var DOMElement
*/
private $rootNode;
/**
* XML document encoding
*
* @var string
*/
private $encoding;
/**
* @var boolean
*/
private $formatOutput = true;
/**
* Set the DOM document encoding.
* @param string $v
*/
public function setEncoding($v) {
$this->encoding = $v;
}
/**
* @param boolean $v
*/
public function setFormatOutput($v) {
$this->formatOutput = (boolean) $v;
}
public function initialize() {
$this->doc = new DOMDocument("1.0", $this->encoding);
$this->rootNode = $this->doc->createElement('results');
$this->doc->appendChild($this->rootNode);
$this->doc->formatOutput = $this->formatOutput;
}
/**
* Processes a specific row from PDO result set.
*
* @param array $row Row of PDO result set.
*/
public function processRow($row) {
$rowNode = $this->doc->createElement('row');
$this->rootNode->appendChild($rowNode);
foreach($row as $columnName => $columnValue) {
$colNode = $this->doc->createElement('column');
$colNode->setAttribute('name', $columnName);
if ($columnValue != null) {
$columnValue = trim($columnValue);
$colNode->nodeValue = $columnValue;
}
$rowNode->appendChild($colNode);
}
}
/**
* Gets a preferred filename for an output file.
*
* If no filename is specified, this is where the results will be placed
* (unless usefile=false).
*
* @return string
*/
public function getPreferredOutfile()
{
return new PhingFile('results.xml');
}
/**
* Write XML to file and free the DOM objects.
*/
public function close() {
$this->out->write($this->doc->saveXML());
$this->rootNode = null;
$this->doc = null;
parent::close();
}
}