J'essaie d'implémenter et d'analyser un document xml en utilisant un service web dans mon application iphone :
Voici ma requête (HTTP GET) :
http://api.stlouisfed.org/fred/category?category_id=125&api_key=78da607fc8224651eca5653e65a4be5e
La réponse sera :
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<category id="125" name="Trade Balance" parent_id="13"/>
</categories>
mon testxml.h :
#import <UIKit/UIKit.h>
@interface testxmlViewController : UIViewController {
IBOutlet UILabel *lbl1;
IBOutlet UILabel *lbl2;
IBOutlet UIButton *submit;
NSMutableData *categoryData;
NSURLConnection *URLConnection;
NSMutableString *currnetCategory;
BOOL *hello;
}
@property (nonatomic, retain) NSMutableData *categoryData;
@property (nonatomic, retain) NSURLConnection *URLConnection;
@property (nonatomic, retain) NSMutableString *currnetCategory;
- (IBAction) submitClick :(id) sender;
@end
mon fichier testxml.m :
#import "testxmlViewController.h"
@implementation testxmlViewController
@synthesize categoryData, URLConnection;
@synthesize currnetCategory;
- (IBAction) submitClick :(id) sender
{
lbl1.text = @"Submit button was clicked.";
static NSString *URLString = @"http://api.stlouisfed.org/fred/category?category_id=125&api_key=78da607fc8224651eca5653e65a4be5e";
NSURLRequest *URLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString]];
URLConnection = [[[NSURLConnection alloc] initWithRequest:URLRequest delegate:self] autorelease];
NSAssert(self.URLConnection != nil, @"Failure to create URL connection.");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.categoryData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[categoryData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"ERROR with theConenction");
[connection release];
[categoryData release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *theXML = [[NSString alloc] initWithBytes: [categoryData mutableBytes] length:[categoryData length] encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXML release];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:categoryData];
if( parser )
{
[parser release];
}
parser = [[NSXMLParser alloc] initWithData: categoryData];
[parser setDelegate: self];
[parser setShouldResolveExternalEntities: YES];
[parser parse];
[connection release];
[categoryData release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(elementName);
if ([elementName isEqualToString:@"category"])
{
lbl1.text = [attributeDict objectForKey:@"name"];
NSLog([attributeDict objectForKey:@"name"]);
//currnetCategory = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (currnetCategory)
{
[currnetCategory appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(elementName);
}
J'obtiens toutes les valeurs imprimées correctement dans mon *. GDB : *
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<category id="125" name="Trade Balance" parent_id="13"/>
</categories>
2010-03-11 10:05:57.817 testxml[16203:207] categories
2010-03-11 10:05:57.819 testxml[16203:207] category
2010-03-11 10:05:57.822 testxml[16203:207] Trade Balance
2010-03-11 10:05:57.823 testxml[16203:207] category
2010-03-11 10:05:57.823 testxml[16203:207] categories
Mais l'application s'est arrêtée après l'exécution de didEndElement méthode.
Pouvez-vous m'aider à résoudre ce problème ?
Merci.