Hallo!
I use this class to parsing XML feed. The parser working right and i get news from feed.

My question - Can I parse feed in HTML table? For example 3 columns.

<?php
class SimpleXmlParser{

// @ Variable Holding Parser

var $SimpleParser;



//@ Feed Source URL

var $feedUrl;



//@ Variables to Hold the Data

var $title = "";

var $description = "";

var $link = "";

var $author="";

var $pubDate="";

var $insideitem = false;

var $tag = "<br>";





// Purpose : Constructor, Which will initialize the XML Parser

function SimpleXmlParser($MyFeed){

    //To begin, I create an instance of the XML parser

    $this->SimpleParser = xml_parser_create();   // creates a new XML parser and returns a reousurce handle referencing 



	$this->feedUrl=$MyFeed;    // Assigns the Feed Source to the Member Variable



	xml_set_object($this->SimpleParser,$this);	  // allows to use parser inside object

	xml_set_element_handler($this->SimpleParser, "XmlParserFirstElement", "XmlParserendElement");	  // Sets the element handler functions for the XML parser parser

	xml_set_character_data_handler($this->SimpleParser, "characterData");   // Sets the character data handler function for the XML parser parser		



	$this->ParseFeed();   // Call to Parser Function

}



function XmlParserFirstElement($parser, $tagName, $attrs) {

    //The Function Will be called, when ever the XML_PARSER Encounters a start Tag, in the XML File

	if ($this->insideitem) {

		$this->tag = $tagName;

	} elseif ($tagName == "ITEM") {

		$this->insideitem = true;

	}

}



function XmlParserendElement($parser, $tagName) {

    //The Function Will be called, when ever the XML_PARSER Encounters a end Tag, in the XML File

	if ($tagName == "ITEM") {

	  	printf("<dt><b><a href='%s'>%s</a></b></dt>",

		trim($this->link),htmlspecialchars(trim($this->title)));    //Display the Title Element from the XML file to HTML

		printf("<dd>%s</dd>",htmlspecialchars(trim($this->description)));  // Description element is made to display in HTML

        // Deallocation of all Global Variables

		$this->title = "";

		$this->description = "";

		$this->link = "";

		$this->insideitem = false;

	}

}	



//Function will be called by the Parser when the end tage of the element is processed. Requires Two Permeters

function characterData($parser, $data) {

    //Permeters: the parser instance and the string containing the data.

	if ($this->insideitem) {

		switch ($this->tag) {

			case "TITLE":

			$this->title .= $data;

			break;

			case "DESCRIPTION":

			$this->description .= $data;

			break;

			case "LINK":

			$this->link .= $data;

			break;

		}

	}

}



function ParseFeed(){

    // This is the Place we need to Do error Handling for the XML file, which is not done in this class

	$fp = fopen($this->feedUrl,"r")       // Open the XML file for reading.

		or die("Oops!!! Unexpected Error While Reading the Feed");   // This part will be executed when we compiler is Unable to Open the XML Feed

    //Starts reading the contents of the file, 4K at a time, and put it in the variable “$data”

	while ($data = fread($fp, 4096)){

		xml_parse($this->SimpleParser, $data , feof($fp));

	}

    //Perform Some Clean Up Work Before Closing the File.

    //Closing the XML File

    fclose($fp);

    //Release the XML Parser

    xml_parser_free($this->SimpleParser);

}

}	

?>

This code output xml file.

<?php

include_once("XmlParser.class.php");

$FeedUrl="http://liternet.bg/feed/e-bulletin.xml";

$XMLpar = new SimpleXmlParser($FeedUrl);

?>

thanks!

[ Mod Edit - bpat1434 ] Added &#91;php&#93;&#91;/php&#93; tags

    You mean take an HTML file and parse it extracting information?

    Yes, in one of a few ways:
    1.) Use DOM
    2.) Complicated Regular Expressions
    3.) Use complicated string cutting mixed with regex

    The last to require that the HTML never changes format. So there will be consistency. The first doesn't require it as much; however, a big change will break it.

    In short, there's no easy way to do it. You probably want to use [man]DOM[/man] to parse the HTML and find the information in much the same way as you did with simpleXML above.

      Write a Reply...