Ask a Question related to PHP Development, Design and Development.
-
Charles Stricklin #1
Q: How do I parse values from a RSS feed?
If I have an RSS newsfeed like this:
<?xml version="1.0" encoding="utf-8"?><!-- generator="whocares" -->
<rss version="0.92">
<channel>
<title>Website Name</title>
<link>http://www.websiteurl.com</link>
<description>Description of website</description>
<lastbuilddate>Sun, 19 Sep 2004 04:34:52 +0000</lastbuilddate>
<docs>http://backend.userland.com/rss092</docs>
<item>
<title>1st Title</title>
<description>1st Description</description>
<link>http://www.websiteurl.com/1stlink/</link>
</item>
<item>
<title>2nd Title</title>
<description>2nd Description</description>
<link>http://www.websiteurl.com/2ndlink/</link>
</item>
</channel>
</rss>
And the following code is used to parse that file/feed:
<?php
$feed = 'http://www.example.com/feed/rss/';
/* ...create and XML parser... */
$xml_parser = xml_parser_create();
/* ...open the feed and parse it... */
$fp = @fopen($feed, 'rb');
if (is_resource($fp)) {
xml_parse_into_struct( $xml_parser, $fp, $vals, $index );
}
@fclose($fp);
/* ...free parser */
xml_parser_free( $xml_parser );
?>
How do I extract the values from $xml_parser?
Charles Stricklin Guest
-
Producing a hash of values from Parse::RecDescent...
Maybe I'll see if anyone in this group has any ideas, since it's really a module question... < original message follows > TIA, -jp DJ... -
Problem to get Parse::Yapp and Parse:Flex working together
A) I did: # cd ~/.cpan/build/Parse-Flex-0.03 # more src/default.y %{ #define YY_DECL char* yyylex YY_PROTO(( void )) #undef yywrap int... -
#25256 [Opn->Bgs]: Parse error: parse error, unexpected $ in ... on line 642
ID: 25256 Updated by: iliaa@php.net Reported By: a dot schat at streamedge dot com -Status: Open +Status: ... -
#25256 [Bgs->Opn]: Parse error: parse error, unexpected $ in ... on line 642
ID: 25256 User updated by: a dot schat at streamedge dot com Reported By: a dot schat at streamedge dot com -Status: ... -
#25256 [NEW]: Parse error: parse error, unexpected $ in ... on line 642
From: a dot schat at streamedge dot com Operating system: Linux PHP version: 4.3.1 PHP Bug Type: Compile Failure Bug... -
Janwillem Borleffs #2
Re: How do I parse values from a RSS feed?
Charles Stricklin wrote:
[...]> If I have an RSS newsfeed like this:[...]> And the following code is used to parse that file/feed:Read the manual closely and you will notice that the second argument of> /* ...open the feed and parse it... */
> $fp = @fopen($feed, 'rb');
> if (is_resource($fp)) {
> xml_parse_into_struct( $xml_parser, $fp, $vals, $index );
> }
> @fclose($fp);
xml_parse_into_struct() should be a string, not a resource.
The following would work better:
$file = file_get_contents($feed);
if ($file) {
xml_parse_into_struct( $xml_parser, $file, $vals, $index );
}
Again, when you read the manual, you will notice that you don't extract the> How do I extract the values from $xml_parser?
values from the resource stored in $xml_parser, but that
xml_parse_into_struct() uses the last two arguments to create an array of
keys and values and an array of indexes which matches the positions of the
elements in the document.
Check the output of print_r() on both arrays to see what I mean.
Please read [url]http://www.php.net/manual/en/ref.xml.php[/url] for more info and
examples.
JW
Janwillem Borleffs Guest



Reply With Quote

