Weitere ähnliche Inhalte Kürzlich hochgeladen (20) 네트워크(웹서비스연결 Xml파싱) pdf2. • XML
Jason
• XML element
• <element> </element>
• IOS NSURL, NSURLRequest, URLConnection
XML
• XML NSXMLParser
4. #import <UIKit/UIKit.h>
@interface TopSongsAppDelegate : NSObject <UIApplicationDelegate> {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
#import "TopSongsAppDelegate.h"
#import "RSSTableViewController.h"
@implementation TopSongsAppDelegate
@synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
RSSTableViewController *tvc = [[[RSSTableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
[self.window setRootViewController:tvc];
[self.window makeKeyAndVisible];
return YES;
}
5. XML title element
title element entry
title element flag
#import <UIKit/UIKit.h>
@interface RSSTableViewController : UITableViewController <NSXMLParserDelegate> {
BOOL waitingForEntryTitle;
NSMutableString *titleString;
NSMutableArray *songs;
NSMutableData *xmlData;
Song List
NSURLConnection *connectionInProgress;
}
- (void) loadSongs;
@end
XML
6. -(void) viewWillAppear:(BOOL)animated
{ TableView
[super viewWillAppear:animated];
NSLog(@"called in viewWillApear");
[self loadSongs];
}
- (void) loadSongs
{
[songs removeAllObjects];
[[self tableView] reloadData];
NSURL *url = [NSURL URLWithString:@"http://ax.itunes.apple.com/" @"WebObjects/MZStoreServices.woa/ws/RSS/topsongs/" @"limit=10/xml"];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
if(!connectionInProgress) {
[connectionInProgress cancel];
[connectionInProgress release];
[xmlData release];
xmlData = [[NSMutableData alloc] init];
NSLog(@"called in loadSongs");
connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
}
7. - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"called in didreceived");
[xmlData appendData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *xmlCheck = [[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"xmlCheck = %@", xmlCheck);
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
[parser setDelegate:self];
[parser parse]; XMLParse XML
XML
[parser release];
[[self tableView] reloadData]; XML songs
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connectionInProgress release];
connectionInProgress = nil;
[xmlData release];
xmlData = nil;
NSString *errorString = [NSString stringWithFormat:@"Fetch failed: %@", [error localizedDescription]];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:errorString delegate:nil cancelButtonTitle:@"OK"
destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:[[self view] window]];
[actionSheet autorelease];
}
8. XML
element tag
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:
(NSDictionary *)attributeDict
{
if([elementName isEqual:@"entry"])
{
NSLog(@"Found a song entry");
waitingForEntryTitle = YES;
}
if([elementName isEqual:@"title"] && waitingForEntryTitle)
{
NSLog(@"found title!");
titleString = [[NSMutableString alloc] init];
}
}
element tag(<title> string </title>
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[titleString appendString:string];
}
element tag(<title> string </title>
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqual:@"title"] && waitingForEntryTitle)
{
NSLog(@"ended title:%@", titleString);
[songs addObject:titleString];
[titleString release];
titleString = nil;
}
if([elementName isEqual:@"entry"])
{
NSLog(@"ended a song entry");
waitingForEntryTitle = NO;
}
}
9. TableView DataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [songs count];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease];
}
[[cell textLabel] setText:[songs objectAtIndex:[indexPath row]]];
return cell;
}
10. xmlCheck = <?xml version="1.0" encoding="utf-8"?>
! <feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom"
xml:lang="en">
! ! <id>http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/
xml</id><title>iTunes Store: Top Songs</title><updated>2011-09-30T14:43:55-07:00</updated><link
rel="alternate" type="text/html" href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?
id=38&popId=1"/><link rel="self" href="http://ax.itunes.apple.com/WebObjects/
MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml"/><icon>http://phobos.apple.com/favicon.ico</
icon><author><name>iTunes Store</name><uri>http://www.apple.com/itunes/</uri></
author><rights>Copyright 2008 Apple Inc.</rights>
11. <entry>
! ! ! ! <updated>2011-09-30T14:43:55-07:00</updated>
! ! ! !
! ! ! ! ! <id>http://itunes.apple.com/us/album/someone-like-you/id420075073?i=420075185&uo=2</id>
! ! ! !
! ! ! ! ! <title>Someone Like You - ADELE</title>
! ! ! !
! ! ! ! !
! ! ! !
! ! ! ! ! <im:name>Someone Like You</im:name>
! ! ! !
! ! ! ! ! <link rel="alternate" type="text/html" href="http://itunes.apple.com/us/album/someone-like-you/id420075073?
i=420075185&uo=2"/>
! ! ! !
! ! ! ! ! <im:contentType term="Music" label="Music"><im:contentType term="Track" label="Track"/></im:contentType>
! ! ! !
! ! ! ! ! <category term="Pop" scheme="http://itunes.apple.com/us/genre/music-pop/id14?uo=2" label="Pop"/>
! ! ! !
! ! ! ! ! <link title="Preview" rel="enclosure" type="audio/x-m4a" href="http://a1.mzstatic.com/us/r1000/051/Music/71/b2/95/
mzi.rsrrzevf.aac.p.m4a" im:assetType="preview"><im:duration>30000</im:duration></link>
! ! ! !
! ! ! ! ! <im:artist href="http://itunes.apple.com/us/artist/adele/id262836961?uo=2">ADELE</im:artist>
! ! ! !
! ! ! ! ! <im:price amount="1.29000" currency="USD">$1.29</im:price>
! ! ! !
! ! ! ! ! <im:image height="55">http://a1.mzstatic.com/us/r1000/014/Music/ea/6f/96/mzi.egqrvlca.55x55-70.jpg</im:image>
! ! ! !
! ! ! ! ! <im:image height="60">http://a5.mzstatic.com/us/r1000/014/Music/ea/6f/96/mzi.egqrvlca.60x60-50.jpg</im:image>
! ! ! !
! ! ! ! ! <im:image height="170">http://a4.mzstatic.com/us/r1000/014/Music/ea/6f/96/mzi.egqrvlca.170x170-75.jpg</im:image>
! ! ! !
! ! ! ! ! <rights>2010 XL Recordings Ltd</rights>
! ! ! !
! ! ! ! ! <im:releaseDate label="February 22, 2011">2011-02-22T00:00:00-07:00</im:releaseDate>
! ! ! !
! ! ! ! ! <im:collection><im:name>21</im:name><link rel="alternate" type="text/html" href="http://itunes.apple.com/us/album/21/
id420075073?uo=2"/><im:contentType term="Music" label="Music"><im:contentType term="Album" label="Album"/></im:contentType></im:collection>
! ! ! !
! ! ! !
! ! ! ! ! <content type="html"><table border="0" width="100%">
<tr>
<td>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td align="center" width="166" valign="top">
<a href="http://itunes.apple.com/us/album/someone-like-you/id420075073?i=420075185&uo=2"><img
border="0" alt="Someone Like You artwork" src="http://a4.mzstatic.com/us/r1000/014/Music/ea/6f/96/mzi.egqrvlca.
170x170-75.jpg" /></a>
</td>
<td width="10"><img alt="" width="10" height="1" src="http://
r.mzstatic.com/images/spacer.gif" /></td>
!<td width="95%">
<b><a href="http://itunes.apple.com/us/album/someone-like-you/id420075073?
i=420075185&uo=2">Someone Like You</a></b><br/>