XML processing and online image display in iPhone

Source: Internet
Author: User

XML processing:

In iPhone development, the XML processing method is very different from. net. I personally feel that it is not as convenient as donot. The nsxmlparser class library is provided in the Apple SDK to parse XML. It requires a URL as the input parameter and parses the XML file through the nsxmlparser delegate method. Nsxmlparser mainly has three delegate methods to parse XML:

1. didstartelement

2. didendelement

3. foundcharacters

Didstartelement: This method is called when the parser object encounters the XML start mark.

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

Didstartelement:
This method is called when the parser object encounters an XML end mark.

-(void)parser:(NSXMLParser*)parser didEndElement:(NSString *)elementName     namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString *)qNamefoundCharacters:

This method is called when the parser finds the character between the start mark and the end mark.

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

Display network images on the iPhone:

There are three steps to display an image through a URL:

1. Create an nsurl object through URL

2. Load image data to an nsdata object through the nsurl object

3. Assign the nsdata object to uiimage

NSURL *url = [NSURL URLWithString:@”www.cnblogs.com\images\sample.jpg”];NSData *data = [NSData dataWithContentsOfURL:url];UIImage *image = [[UIImage alloc] intiWithData:data];

Below we save the image address in an XML file on the server, parse the XML, parse the image address, and then display it.

Create a view based application named xmlwebimages

Open xmlwebimagesviewcontroller. h. Add a uiscrollview object property named webscrollview and add the iboutlet identifier.

#import <UIKit/UIKit.h> @interface XMLWebImageViewController : UIViewController {    IBOutlet UIScrollView *webScrollView;        }@property (nonatomic, retain) IBOutlet UIScrollView * webScrollView;@synthesize scrollView;

Create a class named xmlwebview and inherit the class of uiview. In this class, add the uiimageview attribute object, name it imageview, and add the iboutlet identifier.

@interface XMLWebView : UIView {     IBOutlet UIImageView *imageView;}@property (nonatomic, retain) IBOutlet UIImageView *imageView;@synthesize imageView; 

Create a. XIB file and place a uiimageview on it to associate it with the imageview in the code above.

Create an object class with only one attribute to store the image address:

@interface XMLWebElement: NSObject { UIImage *imgXML;}@property (nonatomic, retain) UIImage * imgXML;@end@synthesize image;

Now we create an XML file from the server, parse the XML file, and display the image code. Open xmlwebimagesviewcontroller. h and create an nsxmlparser object.
An array xmlelements that stores linked object objects. A temporary xmlwebelement object.

@interface XMLWebImagesViewController: UIViewController {     IBOutlet UIScrollView *scrollview;    NSXMLParser *xmlParser;    NSMutableString *currentNode;    NSMutableArray *xmlElements;    XMLWebElement *tempElement;} 

In the viewdidload method, allocate memory for xmlelements and initialize the XML address of the parser object:

- (void)viewDidLoad{     [super viewDidLoad];    xmlElements = [[NSMutableArray alloc] init];    xmlParser = [[NSXMLParser alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://www.cnblogs.com/images.xml"]];    [xmlParser setDelegate:self];     [xmlParser parse];}

The XML structure on the server is as follows: the code for parsing the XML three methods is as follows:

<WebImages>   
<image>
<URL>http://www.cnblogs.com/testimage1.jpg</URL>
</image>
<images>
<URL>http://www.cnblogs.com/testimage2.jpg</URL>
</images>
</WebImages>

The code for parsing the XML three delegate methods is as follows:

- (void)xmlParser:(NSXMLParser *)xmlParser didStartElement: (NSString *)elementName namespaceURI:(NSString *)namespaceURI  qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{    if(![elementName compare:@"PictureInfo"])    {        tempElement = [[iCodeBlogXMLElement alloc] init];    }    else if(![elementName compare:@"imageURL"])    {        currentAttribute = [NSMutableString string];    }    else if(![elementName compare:@"imageTitle"])    {        currentAttribute = [NSMutableString string];    }}- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)   elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{    if(![elementName compare:@"PictureInfo"])    {        [xmlElementObjects addObject:tempElement];    }    else if(![elementName compare:@"imageURL"])    {        NSURL *imageURL = [NSURL URLWithString:currentAttribute];        NSData *data =  [NSData dataWithContentsOfURL:imageURL];        UIImage *image = [[UIImage alloc] initWithData:data];        [tempElement setImage:image];    }    else if(![elementName compare:@"imageTitle"])    {       NSLog(@"The image title is %@", currentAttribute);        [tempElement setImageTitle:currentAttribute];    }    else if(![elementName compare:@"Pictures"])    {        [self layoutSubview];    }}- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{    if(self.currentAttribute)    {        [self.currentAttribute appendString:string];    }} 

In this way, the image address in XML is parsed. The following code shows the image:

NSURL *url = [NSURL URLWithString:urlString];NSData *data = [NSData dataWithContentsOfURL:url];UIImage *image = [[UIImage alloc] intiWithData:data]; 

Summary:This article uses code to describe XML parsing and network slice display in the iPhone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.