Exemple #1
0
 public function toXmlStr($xmlns = null, $xmlname = null)
 {
     if ($xmlns === null) {
         $xmlns = static::NS;
     }
     if ($xmlname === null) {
         $xmlname = static::ROOT;
     }
     $xw = new \XMLWriter();
     $xw->openMemory();
     $xw->setIndent(TRUE);
     $xw->startDocument("1.0", "UTF-8");
     if ($this->PI) {
         $xw->writePI("xml-stylesheet", "type=\"text/xsl\" href=\"" . $this->PI . "\"");
     }
     $this->toXmlWriter($xw, $xmlname, $xmlns);
     $xw->endDocument();
     return $xw->flush();
 }
Exemple #2
0
 function getXML($feed_format = RSS_2_0, $category_filter = null)
 {
     $elementNestLevel = 0;
     //Set the default timezone
     @date_default_timezone_set("GMT");
     //Create the xml write object
     $writer = new XMLWriter();
     //XMLWriter Output method:
     //------------------------------------------------------------------------------------------
     $writer->openMemory();
     //	Xml stored in memory (allows set as variable, output
     //  to file, print/echo	to user, etc.
     //$this->$writer->openURI('php://output');  	//	Send xml to directly to browser/user (not implemented in this version)
     //-----------------------------------------------------------------------------------------
     //XML Version.  Include Charachter Encoding if supplied
     if ($this->feedSpecs['xmlEncodeAs'] != null) {
         $writer->startDocument('1.0', $this->feedSpecs['xmlEncodeAs']);
     } else {
         $writer->startDocument('1.0');
     }
     //Add stylesheet details if provided
     foreach ($this->feedSpecs['feedStylesheet'] as $curStylesheet) {
         $writer->writePI("xml-stylesheet", 'type="' . $curStylesheet['type'] . '" href="' . $curStylesheet['address'] . '"');
     }
     //------------------------------------------
     //Indent level
     $writer->setIndent($this->indent);
     //Validate and display notice if not valid
     if ($this->feedSpecs['enableValidation'] && !$this->validate($feed_format)) {
         //Validation Enabled.  Feed is not valid for the specified output format.
         //echo $err_message;
         //exit;
         $writer->flush();
         $this->invalidFeed($feed_format);
     }
     //Instantiate the FeedConstruct class
     $this->set_feedConstruct($feed_format);
     $this->set_docs($this->feed_construct->docsUrl);
     //Set content type for specified output format
     $this->feed_construct->setHeaderContentType($this->feed_construct->format);
     //Get root, set current as root construct
     $current = $this->feed_construct->getConstruct(ROOT_CONSTRUCT);
     //Start the root element
     $writer->startElement($current['elementName']);
     $elementNestLevel++;
     //add attributes if available
     foreach ($current['attributes'] as $curAttribute) {
         $writer->writeAttribute($curAttribute[1], $curAttribute[3]);
     }
     //add custom namespaces if available
     foreach ($this->feedSpecs['feedXMLNameSpace'] as $curNS) {
         $writer->writeAttribute('xmlns:' . $curNS['prefix'], $curNS['url']);
     }
     //Get root construct children
     $children = $this->feed_construct->getChildren($current['commonName']);
     //Check if has feed channel (sub) element (Channel)
     if (count($children) > 0 && $children[0]['commonName'] == CHANNEL_DATA_CONSTRUCT) {
         //Move to CHANNEL_DATA_CONSTRUCT
         $current = $children[0];
         //Start the element
         $writer->startElement($current['elementName']);
         $elementNestLevel++;
         //add attributes if available
         if ($current['attributes'] !== null) {
             foreach ($current['attributes'] as $curAttribute) {
                 if ($curAttribute[0] != 'default' && isset($this->feedData[$curAttribute[0]])) {
                     //populated with feed data
                     $writer->writeAttribute($curAttribute[1], $this->feedData[$curAttribute[0]]);
                 } elseif ($curAttribute[0] == 'default') {
                     //Populated with constant value
                     $writer->writeAttribute($curAttribute[1], $curAttribute[3]);
                 }
             }
         }
         //Get get feed channel data constructs
         $children = $this->feed_construct->getChildren($current['commonName']);
     }
     $atItemConstruct = false;
     //Loop through Feed Data Elements (stop if reached item construct)
     foreach ($children as $curConstruct) {
         if ($curConstruct['commonName'] == ITEM_CONSTRUCT) {
             $atItemConstruct = true;
             break;
         } else {
             //Test if feed has data for the current construct.  Skip if not.
             if (isset($this->feedData[$curConstruct['commonName']]) && $this->feedData[$curConstruct['commonName']] != null) {
                 $iterator = 0;
                 $mult = true;
                 //Proceed single node in feedData array, or loop through set if multiple.
                 do {
                     if ($curConstruct['max'] > 1 && $iterator >= $curConstruct['max']) {
                         //Allws multiple, but has reached limit
                         $mult = false;
                     } elseif ($curConstruct['max'] != 1) {
                         if (isset($this->feedData[$curConstruct['commonName']][$iterator])) {
                             $this->writeConstruct($writer, $this->feedData[$curConstruct['commonName']][$iterator], $curConstruct, $curConstruct['commonName']);
                             $iterator++;
                         } else {
                             //Reached end of feed data array.
                             $mult = false;
                         }
                     } else {
                         $this->writeConstruct($writer, $this->feedData[$curConstruct['commonName']], $curConstruct, $curConstruct['commonName']);
                         $mult = false;
                     }
                 } while ($mult);
             }
         }
     }
     //Close channel element if required
     if ($atItemConstruct) {
         //Item construct reached when processing feed channel data
         //Items will be added to the channel element
     } else {
         //Reached end of feed data (channel sub elements), but havent reached items
         //Items are outside of the channel element
         $writer->endElement();
         //Close the channel element
         $elementNestLevel--;
         //Add non channel elements to feed if available,
         //exluding items (eg. image, input - RSS 1.0), as they are added
         //separately
         //Move back to parent node if applicable:
         if ($current['parentConstruct'] != null) {
             $current = $this->feed_construct->getConstruct($current['parentConstruct']);
             //Get children.
             $children = $this->feed_construct->getChildren($current['commonName']);
             //Loop through and write element if construct is not item or channel construct (required for RSS 1.0 output)
             foreach ($children as $curConstruct) {
                 if ($curConstruct['commonName'] != ITEM_CONSTRUCT && $curConstruct['commonName'] != CHANNEL_DATA_CONSTRUCT) {
                     //Write element if feed data exists for current
                     if (isset($this->feedData[$curConstruct['commonName']]) && $this->feedData[$curConstruct['commonName']] != null) {
                         $this->writeConstruct($writer, $this->feedData[$curConstruct['commonName']], $curConstruct, $curConstruct['commonName']);
                     }
                 }
             }
         }
     }
     //Add Items to feed xml
     $item_construct = $this->feed_construct->getConstruct(ITEM_CONSTRUCT);
     $item_construct_children = $this->feed_construct->getChildren(ITEM_CONSTRUCT);
     $creditsIncluded = false;
     $itemNumber = 0;
     //Loop through items in feed
     foreach ($this->itemsArray as $currentItem) {
         $itemNumber++;
         if ($item_construct['max'] > 1 && $itemNumber >= $item_construct['max']) {
             //has reached limit
             break;
         }
         //Start the element
         $writer->startElement($item_construct['elementName']);
         //add attributes if available
         if ($item_construct['attributes'] !== null) {
             foreach ($item_construct['attributes'] as $curAttribute) {
                 if ($curAttribute[0] != 'default' && isset($currentItem[$curAttribute[0]])) {
                     //populated with feed data
                     $writer->writeAttribute($curAttribute[1], $currentItem[$curAttribute[0]]);
                 } elseif ($curAttribute[0] == 'default') {
                     //Populated with constant value
                     $writer->writeAttribute($curAttribute[1], $curAttribute[3]);
                 }
             }
         }
         foreach ($item_construct_children as $curItemConstruct) {
             //Test if current feed item has data for the current construct.  Skip if not.
             if (isset($currentItem[$curItemConstruct['commonName']]) && $currentItem[$curItemConstruct['commonName']] != null) {
                 $iterator = 0;
                 $mult = true;
                 $currentFeedData;
                 do {
                     if ($curItemConstruct['max'] > 1 && $iterator >= $curItemConstruct['max']) {
                         //Allws multiple, but has reached limit
                         $mult = false;
                     } elseif ($curItemConstruct['max'] != 1) {
                         if (isset($currentItem[$curItemConstruct['commonName']][$iterator])) {
                             $this->writeConstruct($writer, $currentItem[$curItemConstruct['commonName']][$iterator], $curItemConstruct, $curItemConstruct['commonName']);
                             $iterator++;
                         } else {
                             $mult = false;
                         }
                     } else {
                         $this->writeConstruct($writer, $currentItem[$curItemConstruct['commonName']], $curItemConstruct, $curItemConstruct['commonName']);
                         $mult = false;
                     }
                 } while ($mult);
             }
         }
         //Close the current ITEM_CONSTRUCT element
         $writer->endElement();
     }
     /***************************************************
     		It is a breach of the terms of use to disable, modify or remove the Php FeedWriter footer item if you have not purchased Php FeedWriter for commercial use.
     		For full details, see the Php FeedWriter Terms of Use:    http://phpfeedwriter.webmasterhub.net/terms/         */
     //-Start add footer----------------------
     $this->feed_construct->construct['itemSummary']['type'] = 'html';
     $this->feed_construct->construct['itemContent']['type'] = 'html';
     $item_construct_children = $this->feed_construct->getChildren(ITEM_CONSTRUCT);
     $this->add_credit();
     $writer->startElement($item_construct['elementName']);
     foreach ($item_construct['attributes'] as $curAttribute) {
         $writer->writeAttribute($curAttribute[1], $curAttribute[3]);
     }
     $currentItem = $this->itemsArray[count($this->itemsArray) - 1];
     $creditsIncluded = true;
     foreach ($item_construct_children as $curItemConstruct) {
         if (isset($currentItem[$curItemConstruct['commonName']]) && $currentItem[$curItemConstruct['commonName']] != null) {
             $this->writeConstruct($writer, $currentItem[$curItemConstruct['commonName']], $curItemConstruct, $curItemConstruct['commonName']);
         }
     }
     //End add footer-------------
     //Close remaining elements
     for ($i = $elementNestLevel; $i > 0; $i--) {
         $writer->endElement();
     }
     //End Xml Document
     $writer->endDocument();
     //Output memory if no error
     //(!defined(cr))?exit:null;
     $this->xml = $writer->outputMemory(true);
     //Output the Feed XML if footer included
     //($creditsIncluded)?null:$this->xml = null;
     /*
     if($this->hasCredit)		
     	return $this->xml;
     else
     	return false;
     */
     return $this->xml;
 }