2011-01-18 5 views
0

RSS 피드를 수정하고 싶습니다. 피드에서 X 항목을 제거한 다음 XML로 새 피드를 반환하고 싶습니다.PHP SimpleXML : 피드 수정

<?php 
class RSS { 
    private $simpleXML; 

    public function __construct($address) { 
     $xml = file_get_contents($address); 
     $this->simpleXML = simplexml_load_string($xml); 
    } 

    private function getRssInformation() { 
     // Here I want to get the Rss Head information ... 
    } 

    // Here I get X items ... 
    private function getItems($itemsNumber, $UTF8) { 
     $xml = null; 
     $items = $this->simpleXML->xpath('/rss/channel/item'); 

     if(count($items) > $itemsNumber) { 
      $items = array_slice($items, 0, $itemsNumber, true); 
     } 

     foreach($items as $item) { 
      $xml .= $item->asXML() . "\n"; 
     } 

     return $xml; 
    } 

    public function getFeed($itemsNumber = 5, $UTF8 = true) { 
     // Here i will join rss information with X items ... 
     echo $this->getItems($itemsNumber, $UTF8); 
    } 
} 
?> 

XPath로 가능합니까? 감사합니다.

+0

모든 항목을 설정 해제 할 수 있습니다. unset ($ this-> simpleXML-> channel-> item); 그것은 작동합니다! 고맙습니다. 이제 새 항목을 추가하기 만하면됩니다. – thom

답변

0

또 다른 방법은 그것을 할 수 있습니다 (그러나 청소기 수) :

private function getItems($itemsNumber, $UTF8) { 
    $xml = '<?xml version="1.0" ?> 
       <rss version="2.0"> 
        <channel>'; 

    $i = 0; 

    if (count($this->simpleXML->rss->channel->item)>0){ 
     foreach ($this->simpleXML->rss->channel->item as $item) { 
      $xml .= str_replace('<?xml version="1.0" ?>', '', $item->asXML()); 
      $i++; 
      if($i==$itemsNumber) break; 
     } 
    } 
    $xml .=' </channel></rss> '; 

    return $xml; 
} 

하지만 asXML()을 생각한다; XML 선언을 추가하십시오.

<?xml version="1.0" ?> 

내 코드를 편집했습니다. 그것은 더럽지 만 작동 할 것입니다. 더 좋은 생각이야?

+0

좋습니다. 나는 completly 귀하의 질문을 이해하지 못했지만 그것은 어쨌든 당신을 도울 것으로 보인다 :) – Spir