Getting Error "Trying to get property of non-object"
Posted: Tue Mar 10, 2015 8:51 am
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.
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");
}