<?php /* $Id$ */ $xw = new XMLWriter(); $xw->openMemory(); $xw->setIndent(TRUE); $xw->setIndentString(' '); $xw->startDocument('1.0', "UTF-8"); $xw->startElement('root'); $xw->startElementNS('ns1', 'child1', 'urn:ns1'); $xw->startAttributeNS('ns1', 'att1', 'urn:ns1'); $xw->text('a&b'); $xw->endAttribute(); $xw->writeAttribute('att2', "double\" single'"); $xw->startAttributeNS('ns1', 'att2', 'urn:ns1'); $xw->text("<>\"'&"); $xw->endAttribute(); $xw->writeElement('chars', "special characters: <>\"'&"); $xw->endElement(); $xw->endDocument(); // Force to write and empty the buffer $output = $xw->flush(true); print $output;
/** * Used to export the given data as rss feed * * It returns a xml data string containing rss feed data * * @link http://www.w3schools.com/xml/xml_syntax.asp * @param array $data the data to be exported to rss format * @param array $xml_namespace the xml namespace for the document * prefix => string the xml namespace prefix * name => string the xml namespace name * uri => string the xml namespace uri * @param array $namespace_attributes the list of tags that need to be prefixed with namespace * * @return string $rss_file the contents of the rss file */ public function ExportToRss($data, $xml_namespace, $namespace_attributes) { /** The XMLWriter class object is created. The XMLWriter php extension is enabled by default */ $writer = new \XMLWriter(); $writer->openMemory(); /** The xml prolog is added */ $writer->startDocument('1.0', 'UTF-8'); $writer->setIndent(true); $writer->startAttributeNS($xml_namespace['prefix'], $xml_namespace['name'], $xml_namespace['uri']); /** The rss tag is opened */ $writer->startElement('rss'); $writer->startAttribute('version'); $writer->text('2.0'); $writer->endAttribute(); $writer->startElement('channel'); /** Each Item is added to the rss feed */ for ($count = 0; $count < count($data); $count++) { $data_item = $data[$count]; $writer->startElement('item'); /** Xml tag is created for each data item */ foreach ($data_item as $tag_name => $tag_value) { /** If the tag name is in the list of tags that need to be prefixed with namespace */ if (in_array($tag_name, $namespace_attributes)) { /** The namespace is added to the tag name */ $tag_name = $xml_namespace['name'] . ":" . $tag_name; } $writer->startElement($tag_name); $writer->text($tag_value); $writer->endElement(); } $writer->endElement(); } $writer->endElement(); $writer->endElement(); $writer->endDocument(); /** The xml data is exported to string */ $rss_file = $writer->outputMemory(TRUE); return $rss_file; }
/** * @param string $prefix * @param string $name * @param string $uri * @return bool */ public function startAttributeNS($prefix, $name, $uri = null) { $this->nsArray[$prefix] = $uri; return parent::startAttributeNS($prefix, $name, $uri); }