SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Non-Member john.stone's Avatar
    Join Date
    Aug 2004
    Location
    Houston, Texas
    Posts
    555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pulling a single element from an RSS feed?

    I've wanted to be able to do this for the longest time but I've never figured out how... Any help would be greatly appreciated.

    What I need to do is have an external RSS/XML feed read by my PHP script and have one or two elements pulled out and made into variables.

    The actual feed that I wish to use is here.
    http://wow.allakhazam.com/profile.xml?95570

    Specifically the elements I want to pull are <name> and <level>. Hopefully the solution will be simple enough for me to be able to expand it as needed.

    I think I know how to cache the results, but if someone has already made a function or script that already does this and caches it, that would be fantastic.

    Thanks .

    P.S. In case you're wondering where I've been.. WoW will own your soul... Stay away if you can, but if you can't, play on Cenarius and send me a tell .
    Last edited by john.stone; May 4, 2005 at 19:15.

  2. #2
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i reckon the easiest way will be to use eregi to find the exact text and mark the position then grab woteva is in between the two. sorry its kinda vague but im not great with regex

  3. #3
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're on PHP5, it's just a matter of using DOM. Pretty simple

    PHP Code:
    <?php
     $document 
    = new DOMDocument();
     
    $document->load('http://wow.allakhazam.com/profile.xml?95570');
     
    $info $document->getElementsByTagName('AkzWowProfile')->item(0)->getElementsByTagName('*');
     while(
    $i 0$i $info->length$i++)
     {
         
    $data[$info->tagName] = $info->nodeValue;
     }
     
     
    print_r($data);

  4. #4
    Non-Member john.stone's Avatar
    Join Date
    Aug 2004
    Location
    Houston, Texas
    Posts
    555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MadneM
    i reckon the easiest way will be to use eregi to find the exact text and mark the position then grab woteva is in between the two. sorry its kinda vague but im not great with regex
    Yeah, I was considering trying that... The only thing I worry about is that the file is quite large and it might be kinda rough on the sever.

    Quote Originally Posted by someonewhois
    If you're on PHP5, it's just a matter of using DOM. Pretty simple

    PHP Code:
    <?php
     $document 
    = new DOMDocument();
     
    $document->load('http://wow.allakhazam.com/profile.xml?95570');
     
    $info $document->getElementsByTagName('AkzWowProfile')->item(0)->getElementsByTagName('*');
     while(
    $i 0$i $info->length$i++)
     {
         
    $data[$info->tagName] = $info->nodeValue;
     }
     
     
    print_r($data);
    As always, beaufitul code... But I don't have the luxury of php5. I'm still on 4.3.10. Thanks though .

  5. #5
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That sucks.

    Unfortunately, I can't think of a way to do it in PHP 4 at the moment -- wish I could help further. Sorry.

  6. #6
    Non-Member john.stone's Avatar
    Join Date
    Aug 2004
    Location
    Houston, Texas
    Posts
    555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone else have any ideas?

  7. #7
    Non-Member john.stone's Avatar
    Join Date
    Aug 2004
    Location
    Houston, Texas
    Posts
    555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone at all...?

  8. #8
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, here you go. Spent about 20 minutes trying to get this working -- what a STUPID library PHP4 has here. It basically gives you a function for starting (where it passes you the element's name), and a function for ending (again, passing the name), and a function for the element data itself -- but for some reason, it doesn't give the element's name with the data! Gah. How stupid is that?

    Anyway...

    PHP Code:
    <?php
     set_time_limit
    (0);
     
     
    $a = new XMLData('http://wow.allakhazam.com/profile.xml?95570');
     echo 
    '<pre>';
     
    print_r($a->GetArray());
     
     class 
    XMLData
     
    {
     var 
    $xml;
     var 
    $xmldata;
     var 
    $lastelement;
     var 
    $i 0;
     
         function 
    XMLData($xmlfile)
         {
             
    $this->xml xml_parser_create();
             
    xml_parser_set_option($this->xmlXML_OPTION_CASE_FOLDINGtrue);
             
    xml_set_object($this->xml$this);
             
    xml_set_element_handler($this->xml'XMLStart''XMLEnd');
             
    xml_set_character_data_handler($this->xml'XMLElement');
     
             
    xml_parse($this->xmlfile_get_contents($xmlfile), true);
             
    xml_parser_free($this->xml);
         }
     
         function 
    GetArray()
         {
             return 
    $this->xmldata;
         }
     
     
         function 
    XMLStart($parser$name$attrs)
         {
             if (
    strlen($this->open) > 0)
             {
                 
    $this->nested $this->open;
                 
    $this->i++;
                 
    $this->open $name;
             }
             else
             {
                 
    $this->open $name;
             }
         }
         function 
    XMLEnd($parser$name)
         {
             if (
    $name == $this->nested)
             {
                 
    // Closing the nested.
                 
    unset($this->nested);
             }
             unset(
    $this->open); // Close.
         
    }
         function 
    XMLElement($parser$data)
         {
             if (
    strlen($this->nested) > 0)
             {
                 
    $this->xmldata[$this->nested.$this->i][$this->open] = $data;
             }
             else
             {
                 
    $this->xmldata[$this->open] = $data;
             }
         }
     }
     
     
    ?>
    And:
    Quote Originally Posted by PHP Manual
    These functions are enabled by default, using the bundled expat library. You can disable XML support with --disable-xml. If you compile PHP as a module for Apache 1.3.9 or later, PHP will automatically use the bundled expat library from Apache. In order you don't want to use the bundled expat library configure PHP --with-expat-dir=DIR, where DIR should point to the base installation directory of expat.
    So if your server doesn't have it, it's time to change hosts.

  9. #9
    Non-Member john.stone's Avatar
    Join Date
    Aug 2004
    Location
    Houston, Texas
    Posts
    555
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW! Thank you very, very, very, very, very much!


Bookmarks

Posting Permissions

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