Page 1 of 1

images from XML file

Posted: Fri Dec 17, 2010 8:27 am
by bajere
Hi all,

Very simple question im sure, i want to gather an image from an xml file:

Code: Select all

<story>
<publishdate>17 December 2010</publishdate>
<title>Story 3 title</title>
<content>Lets get this done <strong>2</strong></content>
<image><img src="images/demo.jpg" width="570" height="330" /></image>
</story>
I am using this php method to gather the info, but the images will not show?

Code: Select all

$xml = new SimpleXMLElement($xmlstr);
echo $xml->story[0]->title
echo $xml->story[0]->image
etc
etc
All works as i need, but images just get ignored. Im guessing SimpleXMLElement method cant do what i want. Any ideas on what i can do to get this working. The simplest way possible as its only for internal use.

Any help would be great! Im a real novice, so sorry if this is painfully obvious. :banghead:

Cheers,

Grant

Re: images from XML file

Posted: Fri Dec 17, 2010 10:08 am
by klevis miho
Try with this:
echo $xml->title;
echo $xml->image->img['src'];

Re: images from XML file

Posted: Fri Dec 17, 2010 1:04 pm
by bajere
thanks for the reply, i gave that a bash but no joy. it displays the url of the image, not the image its self... odd

Re: images from XML file

Posted: Fri Dec 17, 2010 1:14 pm
by McInfo
Like $xml, $xml->image->img is a SimpleXMLElement object; so it has an asXML() method.

Code: Select all

echo $xml->image->img->asXML(); // <img src="images/demo.jpg" width="570" height="330"/>

Re: images from XML file

Posted: Fri Dec 17, 2010 1:28 pm
by bajere
ahh, gotcha! works a treat! i knew it was an easy solution! thank you both for your input! :D

MODS= feel free to close this thread, maybe mark it up for other beginners.