I'm following this example in XMLWiter functions, but I'm looking for a way to write the output to a file (sample.xml), I walked through many threads, with no luck. I know that the solution could be easy, but I'm working on PHP only from one month. Can anyone help?
<?php
class XmlConstruct extends XMLWriter
{
/**
* Constructor.
* @param string $prm_rootElementName A root element's name of a current xml document
* @param string $prm_xsltFilePath Path of a XSLT file.
* @access public
* @param null
*/
public function __construct($prm_rootElementName, $prm_xsltFilePath=''){
$this->openMemory(); // change this with $this->openURI('your_file.xml');
$this->setIndent(true);
$this->setIndentString(' ');
$this->startDocument('1.0', 'UTF-8');
if($prm_xsltFilePath){
$this->writePi('xml-stylesheet', 'type="text/xsl" href="'.$prm_xsltFilePath.'"');
}
$this->startElement($prm_rootElementName);
}
/**
* Set an element with a text to a current xml document.
* @access public
* @param string $prm_elementName An element's name
* @param string $prm_ElementText An element's text
* @return null
*/
public function setElement($prm_elementName, $prm_ElementText){
$this->startElement($prm_elementName);
$this->text($prm_ElementText);
$this->endElement();
}
/**
* Construct elements and texts from an array.
* The array should contain an attribute's name in index part
* and a attribute's text in value part.
* @access public
* @param array $prm_array Contains attributes and texts
* @return null
*/
public function fromArray($prm_array){
if(is_array($prm_array)){
foreach ($prm_array as $index => $element){
if(is_array($element)){
$this->startElement($index);
$this->fromArray($element);
$this->endElement();
}
else
$this->setElement($index, $element);
}
}
}
/**
* Return the content of a current xml document.
* @access public
* @param null
* @return string Xml document
*/
public function getDocument(){ //comment this function
$this->endElement();
$this->endDocument();
return $this->outputMemory();
}
/**
* Output the content of a current xml document.
* @access public
* @param null
*/
public function output(){ //comment this function
header('Content-type: text/xml');
echo $this->getDocument();
}
}
//Example:
$contents = array(
'page_title' => 'Generate a XHTML page from XML+XSLT files',
'welcome_msg' => 'Simple XHTML document from XML+XSLT files!',
'prova' => array(
"gino" => array(
"innergino" => "gino inner value"
),
"filo" => "filodata"
),
);
$XmlConstruct = new XmlConstruct('root');
$XmlConstruct->fromArray($contents);
$XmlConstruct->output(); // comment this line
?>