domRSS
Création et parsing de fichier RSS avec l' extension DOM XML.
La classe DOM RSS étend la classe RSS 0.91 en offrant la possibilité de Parser / Créer vos documents RSS grace à l' extension DOM XML de PHP . La documentation est reduite au maximum, en effet l' utilisation est EXACTEMENT la même que pour la classe RSS. Reportez vous donc à la documentation de la classe RSS. Attention cependant l' extension DOM XML est experimentale et son implementation change très rapidement !
Les Méthodes
- DOMRSS - Nouvelle Instance
Exemples
La Source
<?php
class DOMRSS extends RSS {
var $XMLtree;
var $doc;
var $object;
function DOMRSS() {
if(!extension_loaded('domxml'))
die('DOM XML extension is not loaded !!!');
$this->Init_vars();
}
function Parser($file) {
$this->Load_file($file);
}
function Parse() {
$result = $this->My_array($this->domxml_xmlarray($this->XMLtree));
$this->channel['TITLE'] = $this->Get_value($result, 'title');
$this->channel['DESCRIPTION'] = $this->Get_value($result, 'description');
$this->channel['LINK'] = $this->Get_value($result, 'link');
$this->channel['LANGUAGE'] = $this->Get_value($result, 'language');
$this->channel['RATING'] = $this->Get_value($result, 'rating');
$this->channel['COPYRIGHT'] = $this->Get_value($result, 'copyright');
$this->channel['PUBDATE'] = $this->Get_value($result, 'pubdate');
$this->channel['LASTBUILDDATE'] = $this->Get_value($result, 'lastbuilddate');
$this->channel['DOCS'] = $this->Get_value($result, 'docs');
$this->channel['MANAGINGEDITOR'] = $this->Get_value($result, 'managingeditor');
$this->channel['WEBMASTER'] = $this->Get_value($result, 'webmaster');
$this->item = $this-> Get_value($result, 'item', array('link', 'title', 'description'));
$this->image = $this-> Get_value($result, 'image', array('title', 'url', 'link', 'width', 'height', 'description'));
$this->textinput = $this-> Get_value($result, 'textinput', array( 'title', 'description', 'name', 'link'));
$this->skiphours = $this-> Get_value($result, 'skiphours', array('hours'));
$this->skipdays = $this-> Get_value($result, 'skipdays', array('days'));
$infos = array(
'channel' => $this->channel,
'item' => $this->item,
'image' => $this->image,
'textinput' => $this->textinput,
'skiphours' => $this->skiphours,
'skipdays' => $this->skipdays
);
$this->infos = $infos;
return true;
}
function Load_file($file) {
// problems with local files
//$this->XMLtree = domxml_open_file($file);
//or
//die("What' s this file ?");
// $f = implode('', file($file));
$f = file_get_contents($file) or die("What' s this file ?");
// REMOVE !!!!!!!!!!
/*
if(preg_match('`<\?xml(.+?)encoding="(.+?)" *\?>`s', $f, $T))
$this->encoding = $T[2];
$this->encoding = 'iso-8859-1';
echo $T[2];
*/
$this->XMLtree = domxml_open_mem($f);
}
// From PHP.net
function domxml_xmlarray($branch) {
$object = array();
$objptr = &$object;
$branch = $branch->first_child();
while ($branch) {
if (!($branch->is_blank_node())) {
switch ($branch->node_type()) {
case XML_TEXT_NODE: {
//echo $branch->node_value();
//$objptr['cdata'] = $this->encoding!=='utf-8' ? utf8_decode($branch->node_value()) : $branch->node_value();
$objptr['cdata'] = utf8_decode($branch->node_value());
//$objptr['cdata'] = $branch->node_value();
break;
}
case XML_ELEMENT_NODE: {
$objptr = &$object[strtolower($branch->node_name())][];
break;
}
}
if ($branch->has_child_nodes()) {
$objptr = array_merge($objptr, $this->domxml_xmlarray($branch));
}
}
$branch = $branch->next_sibling();
}
return $object;
}
function My_array($arr) {
if(isset($arr['rdf'])) {
$T = $arr['rdf'][0]['channel'][0];
$T['item'] = $arr['rdf'][0]['item'];
return $T;
}
else
return $arr['rss'][0]['channel'][0];
}
function Get_childs_values($arr, $childs) {
$i=0;
foreach($arr as $key => $val) {
foreach($childs as $child) {
$T[$i][strtoupper($child)] = $arr[$key][$child][0]['cdata'];
}
$i++;
}
return $T;
}
function Get_value($arr, $tag, $childs='') {
if(count($arr[$tag])>1) {
//echo count($arr[$tag]).$tag;
$T = $this->Get_childs_values($arr[$tag], $childs);
return $T;
}
if($childs) {
foreach($childs as $val) {
$T[strtoupper($val)] = $this->Get_value($arr[$tag][0], $val);
}
return $T;
}
if(isset($arr[$tag])) {
// Foreach ?????
foreach($arr[$tag] as $key =>$v) {
$T[strtoupper($tag)] = $v['cdata'];
}
return $T[strtoupper($tag)];
}
return '';
}
//--------------------------------//
// CREATOR //
//------------------------------//
function Creator($file) {
$this->File = $file;
$this->i = 0;
$this->channel = array();
$this->image = array();
$this->textinput = array();
$this->skiphours = array();
$this->skipdays = array();
$this->item = array();
}
function Create_tag(&$O, &$branch, $tag, $value='') {
$$tag = $O->create_element($tag);
$$tag = $branch->append_child($$tag);
if($value) {
$$tag{_text} = $O->create_text_node($value);
$$tag{_text} = $$tag->append_child($$tag{_text});
}
}
function Create_file() {
$xmlstr = '<?xml version="1.0" encoding="'.$this->encoding.'" ?>';
if($this->stylesheet_url)
$xmlstr .= '<?xml-stylesheet href="'.$this->stylesheet_url.'" type="text/css"?>';
$xmlstr .= '<!DOCTYPE rss SYSTEM "http://my.netscape.com/publish/formats/rss-0.91.dtd">';
$xmlstr .= '<rss/>';
$doc = domxml_open_mem($xmlstr) or die('err');
//$doc = domxml_new_doc('1.0');
// Root
//$root = $doc->create_element('rss');
//$root = $doc->append_child($root);
//$root -> set_attribute('version', '0.91');
$root = $doc -> document_element();
$root -> set_attribute('version', '0.91');
$channel = $doc->create_element("channel");
$channel = $root->append_child($channel);
$this->Create_tag(&$doc, &$channel, 'title', $this->channel['TITLE']);
$this->Create_tag(&$doc, &$channel, 'description', $this->channel['DESCRIPTION']);
$this->Create_tag(&$doc, &$channel, 'language', $this->channel['LANGUAGE']);
$this->Create_tag(&$doc, &$channel, 'link', $this->channel['LINK']);
if(isset($this->channel['COPYRIGHT']))
$this->Create_tag(&$doc, &$channel, 'copyright', $this->channel['COPYRIGHT']);
if(isset($this->channel['PUBDATE']))
$this->Create_tag(&$doc, &$channel, 'pubdate', $this->channel['PUBDATE']);
if(isset($this->channel['LASTBUILDDATE']))
$this->Create_tag(&$doc, &$channel, 'lastbuilddate', $this->channel['LASTBUILDDATE']);
if(isset($this->channel['DOCS']))
$this->Create_tag(&$doc, &$channel, 'docs', $this->channel['DOCS']);
if(isset($this->channel['MANAGINGEDITOR']))
$this->Create_tag(&$doc, &$channel, 'managingeditor', $this->channel['MANAGINGEDITOR']);
if(isset($this->channel['WEBMASTER']))
$this->Create_tag(&$doc, &$channel, 'webmaster', $this->channel['WEBMASTER']);
if(isset($this->channel['RATING']))
$this->Create_tag(&$doc, &$channel, 'rating', $this->channel['RATING']);
// Manque le textinput
if(!empty($this->skiphours)) {
$skiphours = $doc->create_element('skiphours');
$skiphours = $channel->append_child($skiphours);
foreach($this->skiphours as $hour) {
$this->Create_tag(&$doc, &$skiphours, 'hour', $hour);
}
}
if(!empty($this->skipdays)) {
$skipdays = $doc->create_element('skipdays');
$skipdays = $channel->append_child($skipdays);
foreach($this->skipdays as $day) {
$this->Create_tag(&$doc, &$skipdays, 'day', $day);
}
}
if(!empty($this->image)) {
$image = $doc->create_element('image');
$image = $channel->append_child($image);
$this->Create_tag(&$doc, &$image, 'title', $this->image['TITLE']);
$this->Create_tag(&$doc, &$image, 'link', $this->image['LINK']);
$this->Create_tag(&$doc, &$image, 'url', $this->image['URL']);
if($this->image['WIDTH'] !== '')
$this->Create_tag(&$doc, &$image, 'width', $this->image['WIDTH']);
if($this->image['HEIGHT'] !== '')
$this->Create_tag(&$doc, &$image, 'height', $this->image['HEIGHT']);
if($this->image['DESCRIPTION'] !== '')
$this->Create_tag(&$doc, &$image, 'description', $this->image['DESCRIPTION']);
}
if(!empty($this->item)) {
foreach($this->item as $items) {
$item = $doc->create_element('item');
$item = $channel->append_child($item);
$this->Create_tag(&$doc, &$item, 'title', $items['TITLE']);
$this->Create_tag(&$doc, &$item, 'link', $items['LINK']);
$this->Create_tag(&$doc, &$item, 'description', $items['DESCRIPTION']);
}
}
/*
//echo $doc->dump_mem(true);
echo '<pre>';
//echo print_r($this->channel);
echo htmlentities($doc->dump_mem(true));
echo '</pre>';
*/
$handle = fopen($this->File, 'w');
fputs($handle, $doc->dump_mem(true));
fclose($handle);
}
}// end class
?>
class DOMRSS extends RSS {
var $XMLtree;
var $doc;
var $object;
function DOMRSS() {
if(!extension_loaded('domxml'))
die('DOM XML extension is not loaded !!!');
$this->Init_vars();
}
function Parser($file) {
$this->Load_file($file);
}
function Parse() {
$result = $this->My_array($this->domxml_xmlarray($this->XMLtree));
$this->channel['TITLE'] = $this->Get_value($result, 'title');
$this->channel['DESCRIPTION'] = $this->Get_value($result, 'description');
$this->channel['LINK'] = $this->Get_value($result, 'link');
$this->channel['LANGUAGE'] = $this->Get_value($result, 'language');
$this->channel['RATING'] = $this->Get_value($result, 'rating');
$this->channel['COPYRIGHT'] = $this->Get_value($result, 'copyright');
$this->channel['PUBDATE'] = $this->Get_value($result, 'pubdate');
$this->channel['LASTBUILDDATE'] = $this->Get_value($result, 'lastbuilddate');
$this->channel['DOCS'] = $this->Get_value($result, 'docs');
$this->channel['MANAGINGEDITOR'] = $this->Get_value($result, 'managingeditor');
$this->channel['WEBMASTER'] = $this->Get_value($result, 'webmaster');
$this->item = $this-> Get_value($result, 'item', array('link', 'title', 'description'));
$this->image = $this-> Get_value($result, 'image', array('title', 'url', 'link', 'width', 'height', 'description'));
$this->textinput = $this-> Get_value($result, 'textinput', array( 'title', 'description', 'name', 'link'));
$this->skiphours = $this-> Get_value($result, 'skiphours', array('hours'));
$this->skipdays = $this-> Get_value($result, 'skipdays', array('days'));
$infos = array(
'channel' => $this->channel,
'item' => $this->item,
'image' => $this->image,
'textinput' => $this->textinput,
'skiphours' => $this->skiphours,
'skipdays' => $this->skipdays
);
$this->infos = $infos;
return true;
}
function Load_file($file) {
// problems with local files
//$this->XMLtree = domxml_open_file($file);
//or
//die("What' s this file ?");
// $f = implode('', file($file));
$f = file_get_contents($file) or die("What' s this file ?");
// REMOVE !!!!!!!!!!
/*
if(preg_match('`<\?xml(.+?)encoding="(.+?)" *\?>`s', $f, $T))
$this->encoding = $T[2];
$this->encoding = 'iso-8859-1';
echo $T[2];
*/
$this->XMLtree = domxml_open_mem($f);
}
// From PHP.net
function domxml_xmlarray($branch) {
$object = array();
$objptr = &$object;
$branch = $branch->first_child();
while ($branch) {
if (!($branch->is_blank_node())) {
switch ($branch->node_type()) {
case XML_TEXT_NODE: {
//echo $branch->node_value();
//$objptr['cdata'] = $this->encoding!=='utf-8' ? utf8_decode($branch->node_value()) : $branch->node_value();
$objptr['cdata'] = utf8_decode($branch->node_value());
//$objptr['cdata'] = $branch->node_value();
break;
}
case XML_ELEMENT_NODE: {
$objptr = &$object[strtolower($branch->node_name())][];
break;
}
}
if ($branch->has_child_nodes()) {
$objptr = array_merge($objptr, $this->domxml_xmlarray($branch));
}
}
$branch = $branch->next_sibling();
}
return $object;
}
function My_array($arr) {
if(isset($arr['rdf'])) {
$T = $arr['rdf'][0]['channel'][0];
$T['item'] = $arr['rdf'][0]['item'];
return $T;
}
else
return $arr['rss'][0]['channel'][0];
}
function Get_childs_values($arr, $childs) {
$i=0;
foreach($arr as $key => $val) {
foreach($childs as $child) {
$T[$i][strtoupper($child)] = $arr[$key][$child][0]['cdata'];
}
$i++;
}
return $T;
}
function Get_value($arr, $tag, $childs='') {
if(count($arr[$tag])>1) {
//echo count($arr[$tag]).$tag;
$T = $this->Get_childs_values($arr[$tag], $childs);
return $T;
}
if($childs) {
foreach($childs as $val) {
$T[strtoupper($val)] = $this->Get_value($arr[$tag][0], $val);
}
return $T;
}
if(isset($arr[$tag])) {
// Foreach ?????
foreach($arr[$tag] as $key =>$v) {
$T[strtoupper($tag)] = $v['cdata'];
}
return $T[strtoupper($tag)];
}
return '';
}
//--------------------------------//
// CREATOR //
//------------------------------//
function Creator($file) {
$this->File = $file;
$this->i = 0;
$this->channel = array();
$this->image = array();
$this->textinput = array();
$this->skiphours = array();
$this->skipdays = array();
$this->item = array();
}
function Create_tag(&$O, &$branch, $tag, $value='') {
$$tag = $O->create_element($tag);
$$tag = $branch->append_child($$tag);
if($value) {
$$tag{_text} = $O->create_text_node($value);
$$tag{_text} = $$tag->append_child($$tag{_text});
}
}
function Create_file() {
$xmlstr = '<?xml version="1.0" encoding="'.$this->encoding.'" ?>';
if($this->stylesheet_url)
$xmlstr .= '<?xml-stylesheet href="'.$this->stylesheet_url.'" type="text/css"?>';
$xmlstr .= '<!DOCTYPE rss SYSTEM "http://my.netscape.com/publish/formats/rss-0.91.dtd">';
$xmlstr .= '<rss/>';
$doc = domxml_open_mem($xmlstr) or die('err');
//$doc = domxml_new_doc('1.0');
// Root
//$root = $doc->create_element('rss');
//$root = $doc->append_child($root);
//$root -> set_attribute('version', '0.91');
$root = $doc -> document_element();
$root -> set_attribute('version', '0.91');
$channel = $doc->create_element("channel");
$channel = $root->append_child($channel);
$this->Create_tag(&$doc, &$channel, 'title', $this->channel['TITLE']);
$this->Create_tag(&$doc, &$channel, 'description', $this->channel['DESCRIPTION']);
$this->Create_tag(&$doc, &$channel, 'language', $this->channel['LANGUAGE']);
$this->Create_tag(&$doc, &$channel, 'link', $this->channel['LINK']);
if(isset($this->channel['COPYRIGHT']))
$this->Create_tag(&$doc, &$channel, 'copyright', $this->channel['COPYRIGHT']);
if(isset($this->channel['PUBDATE']))
$this->Create_tag(&$doc, &$channel, 'pubdate', $this->channel['PUBDATE']);
if(isset($this->channel['LASTBUILDDATE']))
$this->Create_tag(&$doc, &$channel, 'lastbuilddate', $this->channel['LASTBUILDDATE']);
if(isset($this->channel['DOCS']))
$this->Create_tag(&$doc, &$channel, 'docs', $this->channel['DOCS']);
if(isset($this->channel['MANAGINGEDITOR']))
$this->Create_tag(&$doc, &$channel, 'managingeditor', $this->channel['MANAGINGEDITOR']);
if(isset($this->channel['WEBMASTER']))
$this->Create_tag(&$doc, &$channel, 'webmaster', $this->channel['WEBMASTER']);
if(isset($this->channel['RATING']))
$this->Create_tag(&$doc, &$channel, 'rating', $this->channel['RATING']);
// Manque le textinput
if(!empty($this->skiphours)) {
$skiphours = $doc->create_element('skiphours');
$skiphours = $channel->append_child($skiphours);
foreach($this->skiphours as $hour) {
$this->Create_tag(&$doc, &$skiphours, 'hour', $hour);
}
}
if(!empty($this->skipdays)) {
$skipdays = $doc->create_element('skipdays');
$skipdays = $channel->append_child($skipdays);
foreach($this->skipdays as $day) {
$this->Create_tag(&$doc, &$skipdays, 'day', $day);
}
}
if(!empty($this->image)) {
$image = $doc->create_element('image');
$image = $channel->append_child($image);
$this->Create_tag(&$doc, &$image, 'title', $this->image['TITLE']);
$this->Create_tag(&$doc, &$image, 'link', $this->image['LINK']);
$this->Create_tag(&$doc, &$image, 'url', $this->image['URL']);
if($this->image['WIDTH'] !== '')
$this->Create_tag(&$doc, &$image, 'width', $this->image['WIDTH']);
if($this->image['HEIGHT'] !== '')
$this->Create_tag(&$doc, &$image, 'height', $this->image['HEIGHT']);
if($this->image['DESCRIPTION'] !== '')
$this->Create_tag(&$doc, &$image, 'description', $this->image['DESCRIPTION']);
}
if(!empty($this->item)) {
foreach($this->item as $items) {
$item = $doc->create_element('item');
$item = $channel->append_child($item);
$this->Create_tag(&$doc, &$item, 'title', $items['TITLE']);
$this->Create_tag(&$doc, &$item, 'link', $items['LINK']);
$this->Create_tag(&$doc, &$item, 'description', $items['DESCRIPTION']);
}
}
/*
//echo $doc->dump_mem(true);
echo '<pre>';
//echo print_r($this->channel);
echo htmlentities($doc->dump_mem(true));
echo '</pre>';
*/
$handle = fopen($this->File, 'w');
fputs($handle, $doc->dump_mem(true));
fclose($handle);
}
}// end class
?>