note 89047 added to ref.xmlwriter

From: Date: Thu, 19 Feb 2009 10:25:00 +0000
Subject: note 89047 added to ref.xmlwriter
Groups: php.notes 
Request: Send a blank email to php-notes+get-150847@lists.php.net to get a copy of this message
I had a feature to the XmlConstruct class by Alexandre Aprica. Now you can use nested array to generate nested xml elements. 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(); $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(){ $this->endElement(); $this->endDocument(); return $this->outputMemory(); } /** * Output the content of a current xml document. * @access public * @param null */ public function output(){ 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(); ---- Server IP: 64.71.164.2 Probable Submitter: 212.48.15.145 ---- Manual Page -- http://www.php.net/manual/en/ref.xmlwriter.php Edit -- https://master.php.net/note/edit/89047 Del: integrated -- https://master.php.net/note/delete/89047/integrated Del: useless -- https://master.php.net/note/delete/89047/useless Del: bad code -- https://master.php.net/note/delete/89047/bad+code Del: spam -- https://master.php.net/note/delete/89047/spam Del: non-english -- https://master.php.net/note/delete/89047/non-english Del: in docs -- https://master.php.net/note/delete/89047/in+docs Del: other reasons-- https://master.php.net/note/delete/89047 Reject -- https://master.php.net/note/reject/89047 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150847) next »