Home » Archives » 14. November 2007

Displaying RSS feeds using PHP (Parsing XML)

November 14, 2007

Hello everybody I've already use this XML parse script using PHP and it really works, now if your tired of searching exact script of parsing XML to PHP now you have this one. Below are the codes I edited I found this in the tutorial and I edited some codes to make this work perfect.

 

<?php

$file = "put your XML data here.xml";
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";

function start($parser, $name, $attrs){
 global $insideitem, $tag, $title, $description, $link;
  
 if ($insideitem) {
  $tag = $name;
 } elseif ($name == "ITEM") {
  $insideitem = true;
 }
}

function ends($parser, $name){
 global $insideitem, $title, $description, $link;
 
 if ($name == "ITEM") {
  printf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link), trim($title));
  printf("<dd>%s</dd><br><br>",trim($description));
  $title = "";
  $description = "";
  $link = "";
  $insideitem = false;
 }
 
}

function character($parser, $data){
 global $insideitem, $tag, $title, $description, $link;
 if ($insideitem) {
 switch ($tag) {
  case "TITLE":
  $title .= $data;
  break;
  case "DESCRIPTION":
  $description .= $data;
  break;
  case "LINK":
  $link .= $data;
  break;
 }
 }
}

?>

<html>
 <head>
 <title></title>
 </head>
 <body>
 <style>
  a{
    font-size: 14px;
  }
 </style>
  <div style="padding:8px; font-family:Arial, Helvetica, sans-serif; font-size:12px;">
   <?
        $xml_parser = xml_parser_create();

  xml_set_element_handler($xml_parser, "start", "ends");
  
  xml_set_character_data_handler($xml_parser, "character");
  
  $fp = fopen($file, "r") or die ("Error:");
  
  while ($data = fread($fp, 4096))
   xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)),
   xml_get_current_line_number($xml_parser)));
  
     
  fclose($fp);
  xml_parser_free($xml_parser);
   ?>
  </div>
 </body>
</html>

 

I hope this article helps.

Feel free to send me comments or suggestions.

Thanks.

Posted by mabuc at 11:32 am | permalink | comments[1]

Who I am?

Hello!

My Name is Mark Abucayon - Filipino Web Developer, Designer, Flash Developer, and Blogger based in Philippines specializing Web Standards CSS XHTML - I love reading tutorials and twit everday.

If you have any inquiries, you may contact me thru email

     

November 2007
M T W T F S S
    Aug »
 1234
567891011
12131415161718
19202122232425
2627282930  

Sponsored Links

Monthly Archives

    

Subscribe

Technorati
Bloglines

Message on the Board

spiker:

This is great and wonderful work mark keep it up..

eM-eM:

awesome work mark!
keep it up..
Godbless ur work.

aileen:

nice site mark!! 2 thumbs up! ;)

Philam:

This is really nice site keep up this work dude

Spike:

This is nice mark- keep up the good work.

Leave a message ▼