1

I am getting an XML error for some reason, even though this(beginner) code does what i want it to do. It fetches strings into an array.

Line 11 results in "Notice: Trying to get property of non-object in D:\pam\w\www\mp\p\lasxml.php on line 11"

    <?php           
    $xml = new DOMDocument( "1.0", "ISO-8859-1" );                                                          
    $xml = simplexml_load_file('http://www.myepisodes.com/rss.php?feed=mylist&showignored=0&sort=asc&uid=Demerzel&pwdmd5=c6ed54b98a82b1----ac58147cedbde5'); 


    $allaFullStringfranxml = array();                                           
    $i = 0;                                                                 
        do{                                                                 
            $allaFullStringfranxml[] = $xml->channel->item[$i]->title;          
            ++$i;                                                               
        }while(($xml->channel->item[$i]->title) != null);                   
2
  • There's a big space in your load_file URI. That shouldn't be there. Commented Dec 12, 2012 at 8:39
  • ah yes, that shouldnt bee there, edited Commented Dec 12, 2012 at 8:43

1 Answer 1

1

I'm not sure why you are using do-while loop, I think in this case foreach will be better - do while loop is running at least once (even if there is no item). Also you are incrementing $i variable and then checking if title title of item with $i index is null - you shouldn't do that without checking if that item really exists. That should work better for you:

foreach ($xml->channel->item as $item) {
    $allaFullStringfranxml[] = (string)$item->title;
}

You can also notice here that I'm doing (string)$item->title - that will convert title node to string, in other case you'll store node object.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! ive been trying foreach, but its kind of abstract as a total beginner.
This should solve your Notice warning too, as it means it won't try and fail to get the final item property. You should accept this answer if it's gone. Welcome to SO!
Yes, it took care of it. :) not sure why i did do-while.. late night trial and error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.