images from XML file

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
bajere
Forum Newbie
Posts: 20
Joined: Fri Jul 03, 2009 6:20 am

images from XML file

Post 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
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: images from XML file

Post by klevis miho »

Try with this:
echo $xml->title;
echo $xml->image->img['src'];
bajere
Forum Newbie
Posts: 20
Joined: Fri Jul 03, 2009 6:20 am

Re: images from XML file

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: images from XML file

Post 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"/>
bajere
Forum Newbie
Posts: 20
Joined: Fri Jul 03, 2009 6:20 am

Re: images from XML file

Post 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.
Post Reply