Getting Error "Trying to get property of non-object"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
btpoole
Forum Newbie
Posts: 7
Joined: Tue Mar 10, 2015 8:32 am

Getting Error "Trying to get property of non-object"

Post by btpoole »

Hi all
Pretty new to PHP but trying to learn. I have written some code that parses html and gathers the img url and inserts into an xml for later use. All this works. I wish to parse the url and obtain the filename and insert it into a url also. I can get the filename out and echo it but can't seem to figure out how to get it to the xml using the code below.

For sake of time and space, not posting all the code just what is necessary.

Code: Select all

	         foreach($images as $img){
		$icon=	$img	->getAttribute('src');
		if( preg_match('/\.(jpg|jpeg|gif)(?:[\?\#].*)?$/i', $icon) ) { //only matching types
		$path_parts = pathinfo($icon);
                $filename=$path_parts['filename'];	//parse url for filename-works
                echo $filename;	
		}
                 $xml->appendChild($root)
                 $id   = $xml->createElement("imageurl");  // IMAGE
                $Fname= $xml->createElement("name");  /FILE NAME
                $idText= $xml->createtextNode($icon);
                $FnameText= $xml->createTextNode($filename->nodeValue);  //ERROR HERE
                $id->appendChild($idText);
                $Fname->appendChild($FnameText);
                $book->appendChild($id);
                $book->appendchild($Fname);
                $root->appendChild($book);
                $xml->formatOutput = true;
                $xml->save("myfile.xml") or die("Error");
                 }
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Getting Error "Trying to get property of non-object"

Post by Celauran »

You haven't mentioned which line it's complaining about specifically, nor can we see where $xml is instantiated, but my guess is that $xml either wasn't instantiated (properly).
btpoole
Forum Newbie
Posts: 7
Joined: Tue Mar 10, 2015 8:32 am

Re: Getting Error "Trying to get property of non-object"

Post by btpoole »

Pretty sure xml is correct in that I use this same code in other projects and xml file is created. When $filename is parse from url I can echo it just fine but when trying to pass it into the line below the error occurs.

It's the line with //ERROR HERE

$FnameText= $xml->createTextNode($filename->nodeValue); //ERROR HERE

xml instantiated
$xml = new DOMDocument("1.0");
$root = $xml->createElement("music");
$book = $xml->createElement("song");
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Getting Error "Trying to get property of non-object"

Post by Celauran »

You're getting $filename from pathinfo. It's a string, not an object.
btpoole
Forum Newbie
Posts: 7
Joined: Tue Mar 10, 2015 8:32 am

Re: Getting Error "Trying to get property of non-object"

Post by btpoole »

Correct, I guess what I should be asking is how to pass the string to the xml.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Getting Error "Trying to get property of non-object"

Post by Celauran »

DOMDocument::createTextNode takes a string as a parameter. Just give it $filename.
btpoole
Forum Newbie
Posts: 7
Joined: Tue Mar 10, 2015 8:32 am

Re: Getting Error "Trying to get property of non-object"

Post by btpoole »

Thank you greatly, I have been reading multiple places and you are the first to show that. Thanks again

After reviewing the DOMText DOMDocument::createTextNode ( string $content ) suggested, I realized that's basically what I have with

Code: Select all

$xml = new DOMDocument("1.0");
                  $root = $xml->createElement("music");
                  $book = $xml->createElement("song");

                  $FnameText= $xml->createtextNode($filename->nodeValue);
 
btpoole
Forum Newbie
Posts: 7
Joined: Tue Mar 10, 2015 8:32 am

Re: Getting Error "Trying to get property of non-object"

Post by btpoole »

I got it, have to remove ->nodeValue and it works.
Post Reply