2012-03-02 88 views
0

我有我的XML解析工作,但我想拿起一個值,實際上我的解析器讀取,並將它們全部寫在NSDictionnary選擇標籤與NSXML分析器的Objective-C解析XML時

讓我告訴你

我在說的xml:XML

正如你所看到的,主標籤是「marche」,有7個標籤「echeance」。在每個標籤中,我們有最後10個日期,每個「日期」包含多個值。

其實,我想獲得這些價值觀念,只有那些價值:

  • 對於每個「echeance」,拿起標籤「echeance」中的attibute「NOM」和「擺平」與變異最近的日期(每個方案)。

這是我得到的結果是:

2012-03-02 20:04:08.576 XML[4497:f803] stories :(
    { 
    echeance = "Mars 2012\n "; 
    settle = "205.25\n  205.5\n  206\n  205\n  202.75\n  204\n  204.75\n  206.5\n  206.25\n  210\n  "; 
    variation = "-0.25\n \n -0.5\n \n 1\n \n 2.25\n \n -1.25\n \n -0.75\n \n -1.75\n \n 0.25\n \n -3.75\n \n 1.5\n \n "; 
}, 
    { 
    echeance = "Juin 2012\n "; 
    settle = "207.75\n  207.5\n  207.25\n  206\n  204.75\n  205.5\n  206.25\n  207.25\n  206.5\n  210.5\n  "; 
    variation = "0.25\n \n 0.25\n \n 1.25\n \n 1.25\n \n -0.75\n \n -0.75\n \n -1\n \n 0.75\n \n -4\n \n 0.75\n \n "; 
}, 
    { 
    echeance = "Ao\U00fbt 2012\n "; 
    settle = "209.75\n  209.25\n  208.5\n  207.25\n  205.75\n  207.25\n  207.75\n  208.75\n  208.5\n  211.75\n  "; 
    variation = "0.5\n \n 0.75\n \n 1.25\n \n 1.5\n \n -1.5\n \n -0.5\n \n -1\n \n 0.25\n \n -3.25\n \n 0.25\n \n "; 
}, 
    { 
    echeance = "Novembre 2012\n "; 
    settle = "188.25\n  188\n  186.75\n  186.25\n  184.75\n  184.75\n  186.5\n  186\n  185\n  188.5\n  "; 
    variation = "0.25\n \n 1.25\n \n 0.5\n \n 1.5\n \n 0\n \n -1.75\n \n 0.5\n \n 1\n \n -3.5\n \n 1\n \n "; 
}, 
    { 
    echeance = "Janvier 2013\n "; 
    settle = "189.75\n  189\n  189.5\n  188.75\n  187.25\n  187.25\n  188.5\n  188.5\n  187.5\n  190\n  "; 
    variation = "0.75\n \n -0.5\n \n 0.75\n \n 1.5\n \n 0\n \n -1.25\n \n 0\n \n 1\n \n -2.5\n \n 0\n \n "; 
}, 
    { 
    echeance = "Mars 2013\n "; 
    settle = "191\n  191\n  191.5\n  190.75\n  189.25\n  189.25\n  190.5\n  190.5\n  189.5\n  192\n  "; 
    variation = "0\n \n -0.5\n \n 0.75\n \n 1.5\n \n 0\n \n -1.25\n \n 0\n \n 1\n \n -2.5\n \n 1\n \n "; 
}, 
    { 
    echeance = "Juin 2013\n "; 
    settle = "194.5\n  194.5\n  195.5\n  194.75\n  193.25\n  193.25\n  194.5\n  194.5\n  193.5\n  196\n  "; 
    variation = "0\n \n -1\n \n 0.75\n \n 1.5\n \n 0\n \n -1.25\n \n 0\n \n 1\n \n -2.5\n \n 3\n \n "; 
} 

正如你可以看到我有一個美麗的NSDictionnary「故事」與各「NOM」「echeance但」擺平「和‘變化’,我有值的所有日期爲每個‘echeance’,而不是隻有最近

如果我的解析器會的工作,我應該有類似的東西。

2012-03-02 20:04:08.576 XML[4497:f803] stories :(
    { 
    echeance = "Mars 2012"; 
    settle = "205.25" 
    variation = "-0.25" 
}, 
    { 
    echeance = "Juin 2012"; 
    settle = "207.75" 
    variation = "0.25" 

..... ..... .....
ETC ...

這裏是我的代碼:

我的.m型號:

#import "XMLParser.h" 

@implementation XMLParser 

@synthesize stories; 
@synthesize delegate; 

- (void)parseXMLAtURL:(NSURL*)path 

{ 
    stories = [[NSMutableArray alloc] init]; 

    textParser = [[NSXMLParser alloc] initWithContentsOfURL:path]; 

    [textParser setDelegate:self]; 

    [textParser parse]; 

} 


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

} 

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 

{ 
    NSString * errorString = [NSString stringWithFormat:@"Impossible de parser le document (Code erreur %i)", [parseError code]]; 

    NSLog(@"erreur de parsing XML: %@", errorString); 

    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Erreur lors du chargement" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

    [errorAlert show]; 
    [errorAlert release]; 

} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
    if(currentElement) 
    { 
     [currentElement release]; 
     currentElement = nil; 
    } 
    currentElement = [elementName copy]; 

    if ([elementName isEqualToString:kExpiry]) 

    { 

     if(item) 
     { 
      [item release]; 
      item = nil; 
     } 
     if(currentExpiry) 
     { 
      [currentExpiry release]; 
      currentExpiry = nil; 
     } 
     if(currentSettle) 
     { 
      [currentSettle release]; 
      currentSettle = nil; 
     } 
     if(currentVariation) 
     { 
      [currentVariation release]; 
      currentVariation = nil; 
     } 

     item = [[NSMutableDictionary alloc] init]; 
     currentVariation = [[NSMutableString alloc] init]; 
     currentSettle = [[NSMutableString alloc] init]; 
     currentExpiry = [[NSMutableString alloc] initWithFormat:[attributeDict objectForKey:@"nom"]]; 


    } 
} 

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

{ 
    if ([elementName isEqualToString:kExpiry]) 

    { 
     [item setObject:currentExpiry forKey:kExpiry]; 
     [item setObject:currentSettle forKey:kCloture]; 
     [item setObject:currentVariation forKey:kChange]; 

     [stories addObject:item]; 

    } 
    //NSLog(@"compteur : %i",compteur); 

} 

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

{ 

    // on sauve les éléments de l' echeancepour l'item en cours 
    if ([currentElement isEqualToString:kCloture]) 
     [currentSettle appendString:string]; 
    else if ([currentElement isEqualToString:kExpiry]) 
     [currentExpiry appendString:string]; 
    else if ([currentElement isEqualToString:kChange]) 
     [currentVariation appendString:string]; 
} 

- (void)parserDidEndDocument:(NSXMLParser *)parser 

{ 

    NSLog(@"C’est fini !"); 
    NSLog(@"stories a %d echeances", [stories count]); 
    NSLog(@"stories :%@", stories); 
    [self performSelectorOnMainThread:@selector(tellTheDelegateItIsFinished) withObject:nil waitUntilDone:NO]; 
} 

- (void) tellTheDelegateItIsFinished { 
    [delegate xmlParserdidFinishParsing]; 
} 

- (void) dealloc 

{ 
    [stories release]; 
    [textParser release]; 
    [item release]; 
    [currentVariation release]; 
    [currentExpiry release]; 
    [currentSettle release]; 

    [super dealloc]; 

} 
@end 

我想我需要「計數」,並挑選了只有我需要的價值,但它不工作

我的另一個問題是「\ n」。我不能夠避免那些「\ n」 :(但主要的問題是撿的「好值」的;)

感謝所有

回答

0

它看起來對我來說,這個問題可能是你並沒有停止你的didEndElement實現中的解析。你有一條線if ([elementName isEqualToString:kExpiry])所以這就是爲什麼你只能得到一個日期。但是你不會那樣做「解決」和「變化」,所以你可以得到所有這些。你可以將它們添加到字典中,並將字典添加到數組中,但這些只是指針,所以數據不斷涌入並且不斷添加到它們中 - 在數組中的字典中。

+0

嗯,好的!你對我有什麼建議! (我很抱歉,我是編程世界的初學者) – Romain 2012-03-02 20:14:53

+0

嗯,我永遠不會按照你的方式構建程序。我會設計出符合數據本質的實際對象,並將整個XML解析爲這些對象。我有一個Marche類和一個Echeance類以及一個Date類和一個Graphe類,我會將整個XML分層結構解析爲一個結構以匹配XML的結構。而且我不會試着像你在做的那樣用一個委託來解析XML;我會使用多委託策略:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/UsingMultipleDelegates.html – matt 2012-03-03 00:11:45