开发者

Objective c read xml and stock it in array

I have a problem with a xlm reader. I have some currency rider I'd like to stock in a array but ther's something wrong in my code.

Here is the feed I'd like to read: http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

header file

@class Convertisseur;


@interface Convertisseur1ViewController : 
   UIViewController <UITextFieldDelegate>{

    IBOutlet UILabel *usd;
    IBOutlet UILabel *euro;
 Convertisseur *convertisseur;


     // parser XML
    NSXMLParser *rssParser;
     // elenco degli elementi letti dal feed
    NSMutableArray *elencoFeed;

     //variabile temporanea pe ogni elemento
    NSMutableDictionary *item;

     // valori dei campi letti dal feed
    NSString *currentElement;
    NSMutableString *currentCube;

    NSArray *currency;
}


//Dichiarazion del parser
 - (void)parseXMLFileAtURL:(NSString *)URL;
@end

Implementation file

#import "Convertisseur1ViewController.h"
#import "Convertisseur.h"

@implementation Convertisseur1ViewController




- (void)parseXMLFileAtURL:(NSString *)URL {
  // inizializziamo la lista degli elementi
 elencoFeed = [[NSMutableArray alloc] init];

  // dobbiamo convertire la stringa "URL" in un elemento "NSURL"
 NSURL *xmlURL = [NSURL URLWithString:URL];

  // inizializziamo il nostro parser XML
 rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

 [rssParser setDelegate:self];

  // settiamo alcune proprietà
 [rssParser setShouldProcessNamespaces:NO];
 [rssParser setShouldReportNamespacePrefixes:NO];
 [rssParser setShouldResolveExternalEntities:NO];

  // avviamo il parsing del feed RSS
 [rssParser parse];
}




- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary开发者_如何学运维 *)attributeDict{   

 currentElement = [elementName copy];
 if ([elementName isEqualToString:@"item"]) {
   // inizializza tutti gli elementi
  item = [[NSMutableDictionary alloc] init];
  currentCube = [[NSMutableString alloc] init];
 }
}

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

 if ([elementName isEqualToString:@"Cube"]) {
  /* salva tutte le proprietà del feed letto nell'elemento "item", per
   poi inserirlo nell'array "elencoFeed" */
  [item setObject:currentCube forKey:@"Cube"];

  [elencoFeed addObject:[item copy]];
 }
}




- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{;
  // salva i caratteri per l'elemento corrente
 if ([currentElement isEqualToString:@"Cube"]){
  [currentCube appendString:string];
 }
}






- (void) parserDidEndDocument:(NSXMLParser *)parser {


 for(int i=1;i<[elencoFeed count];i++) {

  [currency setvalue:[[elencoFeed Objectatindex:i] valueforkey:@"rate"] forkey:[[elencoFeed Objectatindex:i] valueforkey:@"currency"]];
  //currency[i]= [elencoFeed Objectatindex:i] valueforkey:@"rate"] forkey:[[elencoFeed Objectatindex:i] valueforkey:@"currency"];

 }

}



 - (void)viewDidLoad {
 [super viewDidLoad];
 convertisseur = [[Convertisseur alloc] init];


  self.title = @"Convertisseur";

  NSString *path = @"http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
  [self parseXMLFileAtURL:path];
  euro.text =  currency.text;
 }



- (void)viewDidUnload {
  // Release any retained subviews of the main view.
  // e.g. self.myOutlet = nil;
 self.labelEuro = nil;
 self.labelDollar = nil;
 self.convertisseur = nil;

 }



- (void)dealloc {
    [super dealloc];
}


You may consider using TouchXML. I found it a lot easier to use than NSXMLParser

(see http://foobarpig.com/iphone/touchxml-installation-guide.html)

It will give you an in memory representation of your XML document structure, that you will have to walk in order to extract the information you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜