Q: How do I parse values from a RSS feed?

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. #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: ...
    4. #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: ...
    5. #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...
  3. #2

    Default 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:
    [...]
    > /* ...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);
    Read the manual closely and you will notice that the second argument of
    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 );
    }
    > How do I extract the values from $xml_parser?
    Again, when you read the manual, you will notice that you don't extract the
    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139