function actionArray() { echo 'Getting a XML from an ARRAY<br /><br />'; $xml = new YDXml(); $xml->loadString($this->contents); $array = $xml->toArray(); YDDebugUtil::dump(YDXml::traverse($array), 'YDXml::traverse of the ARRAY - static call'); $xml = new YDXml(); // not really needed - loadArray always resets the object before loading an array $xml->loadArray($array); YDDebugUtil::dump($xml->traverse(), 'YDXml::traverse after loading the ARRAY'); }
/** * This function will return the feed in the specified format. The following formats are recognized: RSS0.91, * RSS1.0, RSS2.0, ATOM * * @remark * The default format is "RSS2.0". If you specify no argument indicating the requested format, the "RSS2.0" * format will be used. * * @param $format (optional) The format in which the items should be converted. * * @returns String with the data in the requested format. */ function toXml($format = 'RSS2.0') { // Convert the format to uppercase $format = strtoupper($format); // Check if the format is an allowed one if (!in_array($format, array('RSS0.91', 'RSS1.0', 'RSS2.0', 'ATOM'))) { trigger_error('The YDFeedCreator does not support the format called "' . $format . '". Only the formats "RSS0.91"' . ', "RSS1.0", "RSS2.0" and "ATOM" are supported.', YD_ERROR); } $xml = new YDXml(); $xml->encoding = $this->_encoding; // Formatter for RSS 0.91 if ($format == 'RSS0.91' || $format == 'RSS2.0') { $feed['rss'][0]['#'] = array(); if ($format == 'RSS0.91') { $feed['rss'][0]['@']['version'] = '0.91'; } else { $feed['rss'][0]['@']['version'] = '2.0'; } $feed['rss'][0]['#']['channel'][0]['#'] = array(); $channel =& $feed['rss'][0]['#']['channel'][0]['#']; $channel['title'][0]['#'] = $this->_title; if (!empty($this->_description)) { $channel['description'][0]['#'] = $this->_description; } $channel['link'][0]['#'] = $this->_link; $channel['generator'][0]['#'] = $this->_generator; $i = 0; foreach ($this->_items as $arr) { $channel['item'][$i]['#'] = array(); $item =& $channel['item'][$i]['#']; $item['title'][0]['#'] = $arr['title']; $item['link'][0]['#'] = $arr['link']; $item['guid'][0]['#'] = $arr['guid']; $item['guid'][0]['@']['isPermanlink'] = 'false'; if (!is_null($arr['comments'])) { $item['comments'] = $arr['comments']; } if (!empty($arr['description'])) { $item['description'] = $arr['description']; } if ($format == 'RSS2.0' && !is_null($arr['enclosure'])) { $item['enclosure'][0]['@']['url'] = $arr['enclosure']; $item['enclosure'][0]['@']['length'] = $arr['enclosure_size']; $item['enclosure'][0]['@']['type'] = $arr['enclosure_type']; } $i++; } } // Formatter for RSS1.0 if ($format == 'RSS1.0') { $feed['rdf:RDF'][0]['#'] = array(); $feed['rdf:RDF'][0]['@']['xmlns'] = "http://purl.org/rss/1.0/"; $feed['rdf:RDF'][0]['@']['xmlns:rdf'] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; $feed['rdf:RDF'][0]['@']['xmlns:dc'] = "http://purl.org/dc/elements/1.1/"; $feed['rdf:RDF'][0]['#']['channel'][0]['#'] = array(); $feed['rdf:RDF'][0]['#']['channel'][0]['@']['rdf:about'] = ''; $channel =& $feed['rdf:RDF'][0]['#']['channel'][0]['#']; $channel['title'][0]['#'] = $this->_title; $channel['description'][0]['#'] = $this->_description; $channel['link'][0]['#'] = $this->_link; $channel['items'][0]['#']['rdf:Seq'][0]['#'] = array(); $i = 0; foreach ($this->_items as $item) { $li =& $channel['items'][0]['#']['rdf:Seq'][0]['#']['rdf:li'][$i]; $li['@']['rdf:resource'] = $item['link']; $i++; } $i = 0; foreach ($this->_items as $arr) { $rss['rdf:RDF'][0]['#']['item'][$i]['@']['rdf:about'] = $arr['link']; $item =& $feed['rdf:RDF'][0]['#']['item'][$i]['#']; $item['dc:format'][0]['#'] = 'text/html'; $item['title'][0]['#'] = $arr['title']; $item['link'][0]['#'] = $arr['link']; if (!empty($arr['description'])) { $item['description'][0]['#'] = $arr['description']; } $i++; } } // Formatter for ATOM if ($format == 'ATOM') { $feed['feed'][0]['#'] = array(); $feed['feed'][0]['@']['version'] = "0.3"; $feed['feed'][0]['@']['xmlns'] = "http://purl.org/atom/ns#"; $feed['feed'][0]['#']['title'][0]['#'] = $this->_title; if (!empty($this->_description)) { $feed['feed'][0]['#']['tagline'][0]['#'] = $this->_description; } $feed['feed'][0]['#']['link'][0]['@']['rel'] = 'alternate'; $feed['feed'][0]['#']['link'][0]['@']['type'] = 'text/html'; $feed['feed'][0]['#']['link'][0]['@']['href'] = $this->_link; $feed['feed'][0]['#']['id'][0]['#'] = $this->_link; $feed['feed'][0]['#']['generator'][0]['#'] = $this->_generator; $i = 0; foreach ($this->_items as $arr) { $item =& $feed['feed'][0]['#']['entry'][$i]['#']; $item['title'][0]['#'] = $arr['title']; $item['link'][0]['@']['rel'] = 'alternate'; $item['link'][0]['@']['type'] = 'text/html'; $item['link'][0]['@']['href'] = $arr['link']; $item['id'][0]['#'] = $arr['guid']; if (!empty($arr['description'])) { $item['content'][0]['@']['type'] = 'text/html'; $item['content'][0]['@']['mode'] = 'escaped'; $item['content'][0]['@']['xml:base'] = $arr['link']; $item['content'][0]['#'] = '<![CDATA[' . $arr['description'] . ']]>'; } if (!is_null($arr['enclosure'])) { $item['link'][0]['@']['rel'] = 'enclosure'; $item['link'][0]['@']['href'] = $arr['enclosure']; $item['link'][0]['@']['length'] = $arr['enclosure_size']; $item['link'][0]['@']['type'] = $arr['enclosure_type']; } $i++; } } $xml->loadArray($feed); $xml->encoding = $this->_encoding; return $xml->toString(); }