Ver Mensaje Individual
  #6 (permalink)  
Antiguo 13/12/2006, 15:27
Avatar de ebe
ebe
 
Fecha de Ingreso: marzo-2004
Ubicación: Guatemala
Mensajes: 363
Antigüedad: 21 años
Puntos: 11
Re: Scroll de noticias XML

Bueno, ya que tienes decidido la tecnologia de server, he aqui el codigo que he utilizado para extraer titulo y link de un archivo rss, luego solo quedara el incluir estos datos en el scroll de tu eleccion.

Código PHP:
// Simple enough, the location of the XML file and max headlines to show
$xml_file = "http://www.eldominio.com.gt/rss/rss_actualidad.xml";
$max_news = 5;

// These are both keys that we will use later.
$xml_headline_key = "*RDF:RDF*ITEM*TITLE";
$xml_mylink_key = "*RDF:RDF*ITEM*LINK";

// An array for storing our information. An array is nice to use here
// because it allows us to parse the XML and then temporarily forget about it
// allowing use greater freedom to edit and maniplulate the output.
$story_array = array();

// A counter that will come into use later.
$counter = 0;

// A simple class that will make our life easier. We could use an 
// associative array as well, but I prefer to just write up the class. =)
class xml_story{
    var $headline, $mylink;
}

// Once again, this is what we want our parser to do when it reaches a start tag
function startTag($parser, $data){
    global $current_tag;
    $current_tag .= "*$data";
}

// Same thing here as well. This tells the parser what to do when it trips over an end tag.
function endTag($parser, $data){
    global $current_tag;
    $tag_key = strrpos($current_tag, '*');
    $current_tag = substr($current_tag, 0, $tag_key);
}

// When the parser hits the contents of the tags it will perform this function.
// This will all be explained word for word in the tutorial
function contents($parser, $data){
    global $current_tag, $xml_headline_key, $xml_mylink_key, $counter, $story_array;
    switch($current_tag){
        case $xml_headline_key:
            $story_array[$counter] = new xml_story();
            $story_array[$counter]->headline = $data;
            break;
        case $xml_mylink_key:
            $story_array[$counter]->mylink = $data;
            $counter++;
            break;
    }
}

// Creates the parser
$xml_parser = xml_parser_create();

// Sets the element handlers for the start and end tags
xml_set_element_handler($xml_parser, "startTag", "endTag");

// Sets the data handler, same as before...
xml_set_character_data_handler($xml_parser, "contents");

// Opens the file or gives an error message
$fp = fopen($xml_file, "r") or die("Could not open file");

// Reads the file or gives an error message
$data = '';
while (!feof($fp)) {
    $data .= fread($fp, 8192) or die("Could not read file");
}

// This if statement is exactly the same as before. It parses the xml document
// according to the functions we have defined; and it returns an error message
// if the parsing fails
if(!(xml_parse($xml_parser, $data, feof($fp)))){
    die("Error on line " . xml_get_current_line_number($xml_parser));
}

// Frees up the memory 
xml_parser_free($xml_parser);

// Closes the file
fclose($fp);

//
// THIS IS THE PRINTING CODE
//

// Set max iterations: either total news (if less than $max_news) or previously defined $max_news.
$stop_at = (count($story_array) < $max_news) ? count($story_array) : $max_news;
// A simple for loop that outputs our final data.
    for ($x = 0; $x < $stop_at; $x++) { ?>

        &nbsp;&bull;&nbsp;<a href="<?=$story_array[$x]->mylink;?>" class="newsTitle" style="text-transform:none;" target="_blank"><?=mb_convert_encoding($story_array[$x]->headline'iso-8859-1''UTF-8');?></a><br /><br style="line-height:6px;" />

<?php

    
}

?>
__________________
http://dev.wsnetcorp.com