Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


How to get Image in XML Tag

Hello guys, i got a problem to get an image in XML in <img src=> html tag. Here is the XML data image that i want.

<img src="http://imgs.xkcd.com/comics/xkcd_phone.png" title="abc"/>

how can i use xmlparser in xcode to get this image data from xml description tag and set the image to any view.

1 Answer

Sam Chaudry
Sam Chaudry
25,519 Points

You need to be using the methods in NSXML parser delegate they will allow you to go through the XML file line by line and pull out the image by ID. You could then store it in a NSDictionary and pass in when needed...

So set up NSXML parser and its delegate and you will need the method below.

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

Then go through the XML file element by element and pull out the data you want i.e. something like this

if ([elementName isEqualToString:@"abc"]){

//Store in NSDictionary
if (!abc) abc = [[NSDictionary alloc] init];

// Set current image for NSDictionary Image...

self.XMLImageToDisplay.image=[UIImage imageNamed:[NSDictionary productDictionary objectForKey:@"abc"]];


You could also then call it later and get the image with something like this if for example your using it in a UITableView:

cell.image.image = [UIImage imageNamed:[productDictionary objectForKey:@"abc"]];

Without seeing your code I can't properly comment, I hope this helps or sets you on the right track at least!

thanks Sam Chaudry