• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Having problem getting value from xml tree

Hi,

I'm trying to get a value via a id from a xml file, with id in this code, it is set to a integer from 0 upwards, if I then pass it to the setters below in the constructer (ie  $this->desciption onwards)  it does not work if it is set by the varable, but if I hardcode a number it works fine.

I have checked if the number being passed correctly is as it should be, I think its something obvious, or is this a quirk using this method.
class ReadXmlFile {

    //put your code here
    private $xmlObject, $id;
    private $description, $videoLink, $title, $thumb, $pubDate;

    function __construct($xmlFile, $itemno) {
        $this->id = $itemno;
        $this->xmlObject = simplexml_load_file($xmlFile);
        echo "." . $itemno . ".";
        echo "<pre>";
        var_dump($this->xmlObject->channel->item[$this->id]);
        echo "</pre>";
        $this->desciption = $this->xmlObject->channel->item[$this->id]->description;
        $this->videoLink = $this->xmlObject->channel->item[$this->id]->video_src;
        $this->title = $this->xmlObject->channel->item[$this->id]->title;
        $this->thumb = $this->xmlObject->channel->item[$this->id]->thumbnail_small;
        $this->pubDate = $this->xmlObject->channel->item[$this->id]->pubDate;
    }

    public function getDescription() {
        return $this->desciption;
    }

    public function getVideoLink() {
        return $this->videoLink;
    }

    public function getTitle() {
        return $this->title;
    }

    public function getThumb() {
        return $this->thumb;
    }

    public function getpPubDate() {
        return $this->pubDate;
    }

    public function getCarousel() {

    }

    public function getSearchResults() {
        $count = 0;
        foreach ($this->xmlObject->channel->item as $a) {
            echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?id=' . $count++ . '"><img src="' . $a->thumbnail_small . '" width="144" height="81" alt="' . $a->title . '" /></a></li>';
        }
    }

}

Open in new window

0
darren-w-
Asked:
darren-w-
  • 5
  • 2
1 Solution
 
Ray PaseurCommented:
What is the URL of the XML file?
0
 
darren-w-Author Commented:
Hi, its a local file in a sudirectory
0
 
Ray PaseurCommented:
Then please copy the file and paste it into the code snippet here so we have something to work with.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
darren-w-Author Commented:
np, will do in morning

Darren
0
 
darren-w-Author Commented:
<?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
       >
   <channel>
    <title>icould&#187; icould</title>
    <atom:link href="http://icould.com/api?s=horses&#038;key=ab5a4f1ad484e3bcdf877dd83a24d39f&#038;url=http%3A%2F%2Fwww.careerswales.com" rel="self" type="application/rss+xml" />
    <link>http://icould.com</link>
    <description>It just shows what you can do.</description>
    <lastBuildDate>Thu, 12 May 2011 09:53:29 +0000</lastBuildDate>
    <generator>http://wordpress.org/?v=3.1.1</generator>
    <language>en</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>

      <total_results>16</total_results>
         <item>
            <guid>5363</guid>
            <link>http://icould.com/videos/thomas-hemsley/</link>
            <title>Thomas Hemsley - Stable Staff</title>
            <description><![CDATA[Thomas Hemsley works in a stable for racehorses. He feeds, grooms and rides three horses. He loves his job and hopes to become a professional jockey someday. His grandad started him off in the business after he left school. He enjoyed the work so he did a course at the British Racing School and they found him a job in a yard in Newmarket. You have to like getting up early for this type of work because it starts just after six in the morning. There are low points like injuries to a horse or rider, but the high points make it all worthwhile. &quot;One of my horses won at Royal Ascot this year, which was an amazing feat, absolutely brilliant.&quot; ]]></description>
            <pubDate>Wed, 15 Jul 2009 10:44:53 +0000</pubDate>
            <video_src>http://icould.com/files/videos/5363/156 Thomas Hemsley Extended Version.mp4</video_src>
            <video_width>640</video_width>
            <video_src_height>360</video_src_height>
            <video_embed_height>396</video_embed_height>
            <thumbnail_large>http://icould.com/files/videos/5363/Thomas_Hemsley.jpg</thumbnail_large>
            <thumbnail_small>http://icould.com/files/videos/5363/Thomas_Hemsley-144x81.jpg</thumbnail_small>
                     </item>
                  <item>
            <guid>5920</guid>
            <link>http://icould.com/videos/marita-oloughlin/</link>
            <title>Marita O\&amp;#8217;Loughlin - Equine Lecturer</title>
            <description><![CDATA[Marita O'lochlin is an equine lecturer at the College of Agriculture, Food and Rural Enterprise. She absolutely loves horses and always has done, so this is pretty much her dream job!]]></description>
            <pubDate>Wed, 05 Aug 2009 09:47:52 +0000</pubDate>
            <video_src>http://icould.com/files/videos/5920/marita_olochlin_long.mp4</video_src>
            <video_width>640</video_width>
            <video_src_height>360</video_src_height>
            <video_embed_height>396</video_embed_height>
            <thumbnail_large>http://icould.com/files/videos/5920/marita_olochlin.jpg</thumbnail_large>
            <thumbnail_small>http://icould.com/files/videos/5920/marita_olochlin-144x81.jpg</thumbnail_small>
                     </item>
                  <item>
            <guid>5361</guid>
            <link>http://icould.com/videos/amy-weaver/</link>
            <title>Amy Weaver - Racehorse Trainer</title>
            <description><![CDATA[Amy Weaver has her own business as a Racehorse Trainer. Her big break was literally a big break because she broke her leg falling off a horse while she was working in a casino. While she was resting up she saw her dream job advertised - assistant to a famous racehorse trainer. &quot;I decided that, despite the break, I wanted to get back and work with horses full time and that was it, from then on it was what I wanted to do, and I knew it for a fact then.&quot; She actually got that job, and went on from there to set up her own business. ]]></description>
            <pubDate>Wed, 15 Jul 2009 10:34:50 +0000</pubDate>
            <video_src>http://icould.com/files/videos/5361/155 Amy Weaver Extended Version.mp4</video_src>
            <video_width>640</video_width>
            <video_src_height>360</video_src_height>
            <video_embed_height>396</video_embed_height>
            <thumbnail_large>http://icould.com/files/videos/5361/Amy_Weaver.jpg</thumbnail_large>
            <thumbnail_small>http://icould.com/files/videos/5361/Amy_Weaver-144x81.jpg</thumbnail_small>
                     </item>
                  <item>
            <guid>5089</guid>
            <link>http://icould.com/videos/adrian-longstaffe/</link>
            <title>Adrian Longstaffe - Psychotherapist</title>
            <description><![CDATA[Adrian Longstaffe currently works in Bristol as a Psychotherapist and Consultant in Learning Technology. He began working life as a qualified vet, inspired by his love for horses. In addition to this, he has followed his passion for dance through his work as a professional choreographer.]]></description>
            <pubDate>Fri, 10 Jul 2009 11:14:49 +0000</pubDate>
            <video_src>http://icould.com/files/videos/5089/024 Adrian Longstaffe extended version.mp4</video_src>
            <video_width>640</video_width>
            <video_src_height>360</video_src_height>
            <video_embed_height>396</video_embed_height>
            <thumbnail_large>http://icould.com/files/videos/5089/Adrian_Longstaffe.jpg</thumbnail_large>
            <thumbnail_small>http://icould.com/files/videos/5089/Adrian_Longstaffe-144x81.jpg</thumbnail_small>
                     </item>
                  <item>
            <guid>5366</guid>
            <link>http://icould.com/videos/owen-byrne/</link>
            <title>Owen Byrne - Communications Officer</title>
            <description><![CDATA[Owen Byrne is Communications Officer for the British Horseracing Authority. He deals with the annual report, the website and enquiries from the public. He has to field both nice and angry calls. &quot;When it was the Queen's Golden Jubilee, a parish phoned up wanting to know what her racing colours were, so they could use those colours for the flowers in their Parish Church.&quot; People often phone to complain that jockeys hit the horses too hard. He particularly enjoys working with photographers and designers to promote horse racing in its best light. ]]></description>
            <pubDate>Wed, 15 Jul 2009 10:55:27 +0000</pubDate>
            <video_src>http://icould.com/files/videos/5366/158 Owen Byrne Extended Version.mp4</video_src>
            <video_width>640</video_width>
            <video_src_height>360</video_src_height>
            <video_embed_height>396</video_embed_height>
            <thumbnail_large>http://icould.com/files/videos/5366/Owen_Byrne.jpg</thumbnail_large>
            <thumbnail_small>http://icould.com/files/videos/5366/Owen_Byrne-144x81.jpg</thumbnail_small>
                     </item>
                  <item>
            <guid>5580</guid>
            <link>http://icould.com/job-types/active-leisure-and-learning/john-bramhill/</link>
            <title>John Bramhill - Racehorse Rider</title>
            <description><![CDATA[John Bramhill rides raceshorses for Godolphin, a private racing establishment owned by the crown ruler of Dubai. His passion has always been for horses. &quot;The actual buzz of riding a winner is great. Can't beat that feeling. Can't beat it.&quot; After a few years as a freelance jockey he moved to training, and he is glad he did because Godolphin is &quot;the Ferrari of horse racing&quot;. &quot;Horseracing will never leave my bones and my system, you know, it's part of my soul and I hope to be riding out up until the very last day that I breathe.&quot;]]></description>
            <pubDate>Mon, 27 Jul 2009 12:21:05 +0000</pubDate>
            <video_src>http://icould.com/files/videos/5580/154 John Bramhill Extended Version.mp4</video_src>
            <video_width>640</video_width>
            <video_src_height>360</video_src_height>
            <video_embed_height>396</video_embed_height>
            <thumbnail_large>http://icould.com/files/videos/5580/John_Bramhill.jpg</thumbnail_large>
            <thumbnail_small>http://icould.com/files/videos/5580/John_Bramhill-144x81.jpg</thumbnail_small>
                     </item>
                  <item>
            <guid>5914</guid>
            <link>http://icould.com/videos/george-ussher/</link>
            <title>George Ussher - Technical Services &amp; Environmental Manager </title>
            <description><![CDATA[George Ussher came out of retirement in 2000 to become Technical Services and Environmental Manager for Ulster Carpets Limited. He values education highly - &quot;education really is an absolute must, even more so now than it ever was in my day&quot; and has an Open University degree. George also has a busy life outside of work, keeping a herd of Aberdeen Angus cows and breeding horses for showjumping.]]></description>
            <pubDate>Wed, 05 Aug 2009 09:38:34 +0000</pubDate>
            <video_src>http://icould.com/files/videos/5914/george_ussher_long.mp4</video_src>
            <video_width>640</video_width>
            <video_src_height>360</video_src_height>
            <video_embed_height>396</video_embed_height>
            <thumbnail_large>http://icould.com/files/videos/5914/george_ussher.jpg</thumbnail_large>
            <thumbnail_small>http://icould.com/files/videos/5914/george_ussher-144x81.jpg</thumbnail_small>
                     </item>
                  <item>
            <guid>5369</guid>
            <link>http://icould.com/job-types/active-leisure-and-learning/glen-sparkes/</link>
            <title>Glen Sparkes - Racehorse Riding Instructor</title>
            <description><![CDATA[Glen Sparkes is a Riding Instructor at the British Racing School. He succeeded in getting his jockey's licence, and rode a winner at Lingfield Park on his birthday, but a spine injury brought his career plans to an end. So he applied for a job back at the British Racing School and studied hard to get the right qualifications to be an instructor. &quot;I'm helping these students to progress and to see them improve, and giving them some of my knowledge and experience, and hoping that they can go on and achieve my goal, that I wanted to be a top professional jockey.&quot;]]></description>
            <pubDate>Wed, 15 Jul 2009 10:57:36 +0000</pubDate>
            <video_src>http://icould.com/files/videos/5369/157 Glen Sparkes Extended Version.mp4</video_src>
            <video_width>640</video_width>
            <video_src_height>360</video_src_height>
            <video_embed_height>396</video_embed_height>
            <thumbnail_large>http://icould.com/files/videos/5369/Glen_Sparkes.jpg</thumbnail_large>
            <thumbnail_small>http://icould.com/files/videos/5369/Glen_Sparkes-144x81.jpg</thumbnail_small>
                     </item>
                  <item>
            <guid>5379</guid>
            <link>http://icould.com/videos/shashi-righton/</link>
            <title>Shashi Righton - Jockey\'s Agent</title>
            <description><![CDATA[Shashi Righton is a Jockey's Agent. He was a fairly successful jockey for eleven years, but realised he was never going to ba a champion. So he did a bit of research and decided to become an agent. &quot;My first year of being a Jockey's Agent, I took on a jockey called Micky Fenton, who was lucky enough to get on a horse and win the 1000 Guineas, probably one of the best races in the world.  And we were lucky to win it, and that really was a big turning point in my career, and you know I'll never forget it, it's probably one of the best days of my life.&quot; That gave him a lot of confidence. He now feels he enjoys being an agent even more than he enjoyed being a jockey. ]]></description>
            <pubDate>Wed, 15 Jul 2009 11:34:50 +0000</pubDate>
            <video_src>http://icould.com/files/videos/5379/159 Shashi Righton extended version.mp4</video_src>
            <video_width>640</video_width>
            <video_src_height>360</video_src_height>
            <video_embed_height>396</video_embed_height>
            <thumbnail_large>http://icould.com/files/videos/5379/Shashi_Righton.jpg</thumbnail_large>
            <thumbnail_small>http://icould.com/files/videos/5379/Shashi_Righton-144x81.jpg</thumbnail_small>
                     </item>
                  <item>
            <guid>5509</guid>
            <link>http://icould.com/videos/abbi-collins/</link>
            <title>Abbi Collins - Stunt Coordinator</title>
            <description><![CDATA[Abbi Collins is a Stunt Co-ordinator, Stunt Performer and Horse Master. She says &quot;I'm an adrenalin junkie&quot;. She started as a riding instructor and changed to modelling. She was more interested in active sports and someone suggested &quot;stunts&quot;. Becoming a stunt coordinator requires a lot of on-the-job training.]]></description>
            <pubDate>Mon, 20 Jul 2009 12:29:52 +0000</pubDate>
            <video_src>http://icould.com/files/videos/5509/549 Abbi Collins extended version.mp4</video_src>
            <video_width>640</video_width>
            <video_src_height>360</video_src_height>
            <video_embed_height>396</video_embed_height>
            <thumbnail_large>http://icould.com/files/videos/5509/Abbi_Collins.jpg</thumbnail_large>
            <thumbnail_small>http://icould.com/files/videos/5509/Abbi_Collins-144x81.jpg</thumbnail_small>
                     </item>
               </channel>
   </rss>
      
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (request URI contains query)
Database Caching 67/69 queries in 0.024 seconds using disk

Served from: icould.com @ 2011-05-12 09:54:43 -->

Open in new window

0
 
darren-w-Author Commented:
Ok, Found this solution:
//snippet of code
  function __construct($xmlFile, $itemno) {
        $this->id = $itemno;
        $this->xmlObject = simplexml_load_file($xmlFile);
        $result = $this->xmlObject->channel-> xpath("//item[".$this->id."]");
        $this->desciption = $result[0]->description;
        $this->videoLink = $result[0]->video_src;
        $this->title = $result[0]->title;
        $this->thumb = $result[0]->thumbnail_small;
        $this->pubDate = $result[0]->pubDate;
    }

Open in new window

0
 
darren-w-Author Commented:
Worked out solution
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now