PHP

36
4027

xml php

RSS (Rich Site Summary) is one of the XML applications, in which separate elements (such as news headlines) are presented as completely allocated elements, which allow other sites to get the latest news in that format, in which they wish to receive and display them in their pages in full compliance with the rules of XML-data exchange.
xml phpRSS version 0.91 was developed by Netscape for their network "My Netscape Network", and it allows you to create XML-file, which contains information about the website and also individual elements, which have a "title", "link" and "description". That's great, but how can we after having received RSS-file extract information from it and publish it using HTML? phpEach language allows you to do this by its own, but here we'll use PHP with its built-in XML-parser. PHP uses "expat" library of James Clark, which you already have if you have installed Apache version 1.3.9 or later. To parse XML documents by using PHP, you need to include "with-xml" argument and compile PHP from source codes.
xmlWe'll write a simple script which parses RSS-file, extract information from it, formats it and displays it as a regular HTML. It not only serves as an example of that, how to parse XML in PHP, but it can also be included in any other script to display this information.

The first thing we need to do - is to create class which will store our titles:

class xItem {
var $xTitle;
var $xLink;
var $xDescription;
}

Then we need to define a few global variables for basic information about the website and array to store objects of the header:

$sTitle = "";
$sLink = "";
$sDescription = "";
$arItems = array();
$itemCount = 0;

Then our first two functions:

function startElement($parser, $name, $attrs) {
global $curTag;
$curTag .= "^$name";
}
function endElement($parser, $name) {
global $curTag;
$caret_pos = strrpos($curTag,^);
$curTag = substr($curTag,0,$caret_pos);
}

To parse XML in PHP, you need to define the functions, which are called in such cases:

  • parser encounters a starting element of the tag;
  • parser encounters a end element of the tag;
  • parser encounters data between the start and end tags.

We will issue them as follows: by setting the global variable ($curTag) as a string, which contains all the parent tags, separated by ^. For example, for such XML-structure:

variable $curTag will looks like:
^RSS^CHANNEL^ITEM

All we need to do - is to find out when the parser will meet the right $curTag, and extract data in accordance with it. All this is done in the characterData function. Here it is:
function characterData($parser, $data) {
global $curTag; // initially channel information
global $sTitle, $sLink, $sDescription;
$titleKey = "^RSS^CHANNEL^TITLE";
$linkKey = "^RSS^CHANNEL^LINK";
$descKey = "^RSS^CHANNEL^DESCRIPTION";
if ($curTag == $titleKey) {
$sTitle = $data;
} elseif ($curTag == $linkKey) {
$sLink = $data;
} elseif ($curTag == $descKey) {
$sDescription = $data;
}
// now we get the elements
global $arItems, $itemCount;
$itemTitleKey = "^RSS^CHANNEL^ITEM^TITLE";
$itemLinkKey = "^RSS^CHANNEL^ITEM^LINK";
$itemDescKey = "^RSS^CHANNEL^ITEM^DESCRIPTION";
if ($curTag == $itemTitleKey) {
// create a new xItem
$arItems[$itemCount] = new xItem();
// set the properties of the new element
$arItems[$itemCount]->xTitle = $data;
} elseif ($curTag == $itemLinkKey) {
$arItems[$itemCount]->xLink = $data;
} elseif ($curTag == $itemDescKey) {
$arItems[$itemCount]->xDescription = $data;
// increase the counter
$itemCount++;
}
}

xml htmlThis function checks whether the $curTag contains necessary for us string, and if its so, then extracts data from it and assigned to variables. Initially it extracts the basic information about website, then checks if there are elements. If there are, then it creates xItem, inserts it into the array $arItems and sets the properties for the corresponding data in the RSS-file.

Now when functions are defined, we use the standard in PHP way for communication with XML-parser:

// basic cycle
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($uFile,"r"))) {
die ("Can not get RSS");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);

Anything that in the above code begins with "xml_" - is a standard XML-functions in PHP. We report a parser that our functions must be fulfilled, when it meets: the start tag, the end tag, or data, and then we load the RSS file (the variable $uFile must be set on the correct RSS-file) and then we run the parser (xml_parse).

Now when our data is disassembled on individual variables, its not difficult to transform them into HTML:

xml phpWe have added some variables for description, font, its size.
When it comes to data exchange, its difficult to oppose anything to XML. Defining of XML-format, which can be used by many people (like RSS) - is just one of the benefits of this complex but elegant technology.

Full source code look here:

class xItem {
var $xTitle;
var $xLink;
var $xDescription;
}

// general vars
$sTitle = "";
$sLink = "";
$sDescription = "";
$arItems = array();
$itemCount = 0;

// ********* Start User-Defined Vars ************
// rss url goes here
$uFile = "http://www.wirelessdevnet.com/wirelessnews/rss/dailynews.rss";
// descriptions (true or false) goes here
$bDesc = true;
// font goes here
$uFont = "Verdana, Arial, Helvetica, sans-serif";
$uFontSize = "2";
// ********* End User-Defined Vars **************

function startElement($parser, $name, $attrs) {
global $curTag;

$curTag .= "^$name";

}

function endElement($parser, $name) {
global $curTag;

$caret_pos = strrpos($curTag,^);

$curTag = substr($curTag,0,$caret_pos);

}

function characterData($parser, $data) { global $curTag; // get the Channel information first
global $sTitle, $sLink, $sDescription;
$titleKey = "^RSS^CHANNEL^TITLE";
$linkKey = "^RSS^CHANNEL^LINK";
$descKey = "^RSS^CHANNEL^DESCRIPTION";
if ($curTag == $titleKey) {
$sTitle = $data;
}
elseif ($curTag == $linkKey) {
$sLink = $data;
}
elseif ($curTag == $descKey) {
$sDescription = $data;
}

// now get the items
global $arItems, $itemCount;
$itemTitleKey = "^RSS^CHANNEL^ITEM^TITLE";
$itemLinkKey = "^RSS^CHANNEL^ITEM^LINK";
$itemDescKey = "^RSS^CHANNEL^ITEM^DESCRIPTION";

if ($curTag == $itemTitleKey) {
// make new xItem
$arItems[$itemCount] = new xItem();

// set new item objects properties
$arItems[$itemCount]->xTitle = $data;
}
elseif ($curTag == $itemLinkKey) {
$arItems[$itemCount]->xLink = $data;
}
elseif ($curTag == $itemDescKey) {
$arItems[$itemCount]->xDescription = $data;
// increment item counter
$itemCount++;
}
}

// main loop
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($uFile,"r"))) {
die ("could not open RSS for input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);

// write out the items
?>

for ($i=0;$i $txItem = $arItems[$i];
?>
xTitle); ?>

if ($bDesc) {
?>
xDescription); ?>

}
echo ("
");
}

initially channel information
Author: World of translation
RATE PUBLICATION
5 (votes: 0)

Комментарии:

  • avatar
    Jeff - 16.12.2012, 11:07
    Some genuinely wonderful posts on this internet site, appreciate it for contribution.
  • avatar
    Dopler - 11.01.2013, 01:22
    Nice post.
  • avatar
    Mikaela - 25.01.2013, 05:01
    Nice post. I learn something much more difficult on distinct blogs everyday. It's going to generally be stimulating to read content from other writers and practice a bit some thing from their store. I'd prefer to use some using the content on my blog no matter whether you do not mind. Thanks for sharing.
  • avatar
    Tirunesh - 5.03.2013, 08:30
    This internet website is honestly a walk-through for all of the information you wanted about this and didn't know who to ask. Glimpse here, and you'll surely discover it.
  • avatar
    TitTitGeall - 29.03.2013, 05:25
    Superb Post.thanks for share..much more wait ..
  • avatar
    John - 30.03.2013, 04:02
    I discovered your blog web-site on google and check a couple of of your early posts. Continue to maintain up the really very good operate. I just extra up your RSS feed to my MSN News Reader. Searching for forward to reading even more from you later on!
  • avatar
    Denny - 30.03.2013, 04:27
    I'm impressed, I need to say. Genuinely rarely do I encounter a weblog that's both educative and entertaining, and let me tell you, you have hit the nail on the head.
  • avatar
    Juna - 2.04.2013, 13:41
    Hello! I just would like to give a huge thumbs up for the amazing info you've got here on this post. I are going to be coming back to your weblog for extra soon.
  • avatar
    TitTitGeall - 20.04.2013, 18:31
    I discovered your weblog site on google and check a number of of your early posts. Continue to maintain up the highly fine operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading alot more from you later on! Nike Lunar Elite 2 for women
  • avatar
    TitTitGeall - 20.04.2013, 18:57
    There is certainly noticeably a bundle to know about this. I assume you made certain nice points in features also. rabat Nike Free 7.0
  • avatar
    TitTitGeall - 20.04.2013, 21:04
    One can find some intriguing points in time in this article but I don't know if I see all of them center to heart. There is certainly some validity but I will take hold opinion until I look into it further. Very good article , thanks and we want a lot more! Added to FeedBurner also Nike Free Run 3 til salg
  • avatar
    TitTitGeall - 20.04.2013, 21:32
    pretty nice post, i surely love this web page, maintain on it Nike Lunar Elite til billig
  • avatar
    nagunindink - 21.04.2013, 00:31
    Amazing Post.thanks for share..a lot more wait .. red bottom heels
  • avatar
    Juggipsyporgo - 25.04.2013, 01:16
    Can I just say what a relief to uncover someone who essentially knows what theyre talking about on the web. You definitely know ways to bring an problem to light and make it critical. Alot more men and women have to read this and understand this side of the story. I cant think youre not more popular considering that you certainly have the gift. red bottom heels
  • avatar
    Juggipsyporgo - 25.04.2013, 02:51
    It is hard to get knowledgeable people today on this topic, but you sound like you know what you're talking about! Thanks michael kors discount bags
  • avatar
    actishhic - 9.05.2013, 21:49
    This actually answered my challenge, thank you! micael kors
  • avatar
    actishhic - 9.05.2013, 22:07
    Spot on with this write-up, I really feel this web-site wants a lot more consideration. I'll in all probability be once more to read far more, thanks for that information. michael kors sale handbags
  • avatar
    actishhic - 9.05.2013, 23:27
    particularly nice post, i undoubtedly love this website, keep on it michal kors
  • avatar
    actishhic - 9.05.2013, 23:48
    Nice post. I learn something more challenging on numerous blogs everyday. It will constantly be stimulating to read content from other writers and practice a little some thing from their store. I'd prefer to use some with the content on my weblog no matter if you don't mind. Natually I'll provide you with a link on your web weblog. Thanks for sharing. michael kors tote bag
  • avatar
    nagunindink - 16.05.2013, 09:38
    Aw, this was a honestly nice post. In concept I would like to put in writing like this in addition - taking time and actual effort to make a really good article?- but what can I say?- I procrastinate alot and by no indicates seem to obtain some thing accomplished. red bottom shoes
  • avatar
    nagunindink - 16.05.2013, 09:52
    I was especially pleased to locate this web-site.I wanted to thanks for your time for this fantastic read!! I absolutely enjoying every little bit of it and I have you bookmarked to check out new stuff you weblog post. shoes with red soles
  • avatar
    nagunindink - 16.05.2013, 10:50
    This is the appropriate blog for anyone who desires to find out about this topic. You recognize so significantly its almost difficult to argue with you (not that I essentially would want?-HaHa). You definitely put a brand new spin on a subject thats been written about for years. Very good stuff, just wonderful! red sole shoes
  • avatar
    actishhic - 16.05.2013, 19:39
    You produced some decent points there. I looked on the internet for the concern and discovered most individuals will go along with with your web page. michael kors outlet houston
  • avatar
    actishhic - 16.05.2013, 19:51
    quite nice post, i definitely love this web site, keep on it michael kors bag
  • avatar
    actishhic - 16.05.2013, 20:27
    you might have an excellent blog here! would you like to make some invite posts on my blog? michael kors wallets
  • avatar
    actishhic - 16.05.2013, 20:41
    You'll find definitely a whole lot of details like that to take into consideration. Which is an excellent point to bring up. I present the thoughts above as general inspiration but clearly you'll find questions like the one you bring up where probably the most vital factor is going to be working in honest superior faith. I don?t know if most desirable practices have emerged about items like that, but I'm certain that your job is clearly identified as a fair game. Both boys and girls feel the impact of just a moment's pleasure, for the rest of their lives. michael kors handbags outlet online
  • avatar
    pypeFolillVep - 17.05.2013, 11:33
    I'd have to check with you here. Which just isn't something I typically do! I take pleasure in reading a post which will make folks feel. Also, thanks for allowing me to comment! red bottom shoes
  • avatar
    pypeFolillVep - 17.05.2013, 12:18
    Can I just say what a relief to come across a person who truly knows what theyre talking about on the internet. You surely know the right way to bring an problem to light and make it important. A lot more consumers have to read this and fully grasp this side of the story. I cant believe youre not additional well-liked due to the fact you surely have the gift. red bottom heels
  • avatar
    pypeFolillVep - 20.05.2013, 01:56
    I'd need to check with you here. Which just isn't some thing I often do! I take pleasure in reading a post that can make persons think. Also, thanks for allowing me to comment! new era hats
  • avatar
    pypeFolillVep - 20.05.2013, 02:04
    It is best to take component in a contest for among the preferred blogs on the internet. I will recommend this web site! cheap hats
  • avatar
    pypeFolillVep - 20.05.2013, 02:31
    The next time I read a weblog, I hope that it doesnt disappoint me as significantly as this 1. I mean, I know it was my selection to read, but I in fact thought youd have some thing fascinating to say. All I hear is usually a bunch of whining about some thing which you could fix for those who werent too busy seeking for attention. cheap hats
  • avatar
    pypeFolillVep - 20.05.2013, 02:40
    I'm regularly to blogging and i seriously appreciate your content. The article has definitely peaks my interest. I'm going to bookmark your web page and keep checking for new specifics. new era caps
  • avatar
    actishhic - 20.05.2013, 04:07
    Can I just say what a relief to find an individual who truly knows what theyre talking about on the web. You absolutely know methods to bring an issue to light and make it fundamental. Much more individuals should read this and understand this side of the story. I cant think youre not a lot more favorite due to the fact you unquestionably have the gift. new era hats
  • avatar
    actishhic - 20.05.2013, 04:16
    Aw, this was a definitely nice post. In thought I would like to put in writing like this moreover - taking time and actual effort to make a extremely decent article?- but what can I say?- I procrastinate alot and by no means seem to obtain some thing done. new era hats
  • avatar
    actishhic - 20.05.2013, 04:58
    I'd have to check with you here. Which just isn't something I often do! I take pleasure in reading a post which will make people feel. Also, thanks for permitting me to comment! new era hats
  • avatar
    actishhic - 20.05.2013, 05:12
    An interesting discussion is worth comment. I feel that you really should write much more on this topic, it might not be a taboo topic but normally people today are not sufficient to speak on such topics. To the next. Cheers new era hats
Go UP