NSXmlParser error

Discussion in 'iOS Programming' started by leconteconte, Feb 28, 2013.

  1. leconteconte macrumors newbie

    Joined:
    Feb 28, 2013
    #1
    Hi
    want to tell you that i'm a beginner so be ...
    i want to parse this XML File to get their tag and attributs

    Code:
    <commands>
       <command id="84" crypt="0">
      <NbreSecEnCours>2</NbreSecEnCours>
      <IdSession NomAppli="Notepad" NameMach="T2037" GuidSession="12729c94-1bf6-49ac-beca-e3c99609aded">2</IdSession>
      <IdSession NomAppli="nslookup" NameMach="T2037" GuidSession="12729c94-1bf6-49ac-beca-e3c99609aded">2</IdSession>
      </command>
      </commands>
    
    that is why i try to do that. that is my class for parsing the previous XML file

    Code:
    //--------------------------------------------------------------------
    // Start of element
    - (void)parser:(NSXMLParser *)parser
    didStartElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI
     qualifiedName:(NSString *)qName
        attributes:(NSDictionary *)attributeDict
    {
        currentElement = [elementName copy];
        //currentElement = [attributeDict copy];
        //NSLog(@"%@", currentElement);
          item2 = [[NSMutableDictionary alloc]init];
        
          item = [[NSMutableDictionary alloc]init];
        
        if ([elementName isEqualToString:@"command"]) {
            
            //item utilisé pour le stockage du  nombre de session
         
            
            //currentNode = [[NSMutableString alloc]init];
            
            //Récupération du numéro de la requete
            
        NSLog(@"id = %@",[attributeDict objectForKey:@"id"]);
        currentNbreSecEnCours = [[NSMutableString alloc]init];
        currentIdSession = [[NSMutableString alloc]init];
        [item setObject:currentNbreSecEnCours forKey:@"NbreSecEnCours"];
          
       
        [commandes addObject:item];
        //[commandes addObject:item2];
            XMLItems = [[NSMutableDictionary alloc]init];
            
        }
         
        if([elementName isEqualToString:@"IdSession"]){
            
                  
            //[item2 setObject:currentIdSession forKey:@"IdSession"];
    
            
           NomAppli = [attributeDict objectForKey:@"NomAppli"];
           NameMach = [attributeDict objectForKey:@"NameMach"];
           GuidSession = [attributeDict objectForKey:@"GuidSession"];
            [item2 setObject:NomAppli forKey:@"NomAppli"];
            [item2 setObject:NameMach forKey:@"NameMach"];
            [item2 setObject:GuidSession forKey:@"GuidSession"];
            [commandes addObject:item2];
            /*[commandes addObject:NomAppli];
            [commandes addObject:NameMach];
            [commandes addObject:GuidSession];*/
            NSLog(@"[didStartElement]commandes: %@",commandes);
        }
        
    
    }
    
    // Found Character
    - (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
      
        if ([currentElement isEqualToString:@"NbreSecEnCours"]) {
            [currentNbreSecEnCours appendString:string];
            NSLog(@"currentNbreSecEncours: %@", currentNbreSecEnCours);
        }
        if ([currentElement isEqualToString:@"IdSession"]) {
            [currentIdSession appendFormat:string];
            NSLog(@"currentIdSession:  %@", currentIdSession);
           
        }
    }
    
    // End Element
    - (void) parser:(NSXMLParser *)parser
      didEndElement:(NSString *)elementName
       namespaceURI:(NSString *)namespaceURI
      qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
       if ([elementName isEqualToString:@"command"]) {
            //[item setObject:currentNode forKey:@"Username"];
            [item setObject:currentNbreSecEnCours forKey:@"NbreSecEnCours"];
            [commandes addObject:item];
        }
        if([elementName isEqualToString:@"IdSession"]){
            [item2 setObject:currentIdSession forKey:@"IdSession"   ];
            NomAppli = [attributeDict objectForKey:@"NomAppli"];
            NameMach = [attributeDict objectForKey:@"NameMach"];
            GuidSession = [attributeDict objectForKey:@"GuidSession"];
            
          
            NSLog(@"[didEndElement]commandes: %@",commandes);
        }
    }
    
    //------------------------------------------------------------------
    //-(NSString*)getElementValue:(NSString*)Node{
    -(NSMutableArray*)getElementValue{
        //NSString *nodeValue;
        NSString *nbreSess;
        NSString *idSession;
        NSString *nomAppli;
        NSString *nameMach;
        NSString *guidSession;
        NSMutableArray *results = [[NSMutableArray alloc]init];
        
        
        NSLog(@"Commande=> %@",commandes);
        
        for (NSMutableDictionary *val in commandes) {
           
            nbreSess = [val objectForKey:@"NbreSecEnCours"];
            nbreSess = [nbreSess stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            nbreSess = [nbreSess stringByReplacingOccurrencesOfString:@"\t" withString:@""];
            [results addObject:nbreSess];
            
            
            
            idSession = [val objectForKey:@"IdSession"];
            idSession = [idSession stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            idSession = [idSession stringByReplacingOccurrencesOfString:@"\t" withString:@""];
            [results addObject:idSession];
    
            
            
          nomAppli = [val objectForKey:@"NomAppli"];
           nomAppli = [nomAppli stringByReplacingOccurrencesOfString:@"\n" withString:@""];
           nomAppli = [nomAppli stringByReplacingOccurrencesOfString:@"\t" withString:@""];
            [results addObject:nomAppli];
    
            
            nameMach = [val objectForKey:@"NameMach"];
            nameMach = [nameMach stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            nameMach = [nameMach stringByReplacingOccurrencesOfString:@"\t" withString:@""];
            [results addObject:nameMach];
            
            
            
            guidSession = [val objectForKey:@"GuidSession"];
            guidSession = [guidSession stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            guidSession = [guidSession stringByReplacingOccurrencesOfString:@"\t" withString:@""];
            [results addObject:guidSession];
            
        
           
            
        }
        NSLog(@"Results=> %@",results);
        //return nodeValue;
        //return commandes;
        return results;
    }
    

    But in console i have this error and i'm not understand Why and how to do to resolve it

    Code:
    2013-02-28 09:12:45.535 XMLTesteur-1.1[666:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
    *** First throw call stack:
    (0x1ca0012 0x10dde7e 0x1c53b6a 0x1c53a20 0x85d7 0x561f 0x5a7e 0x10f1705 0x28920 0x288b8 0xe9671 0xe9bcf 0xe8d38 0x5833f 0x58552 0x363aa 0x27cf8 0x1bfbdf9 0x1bfbad0 0x1c15bf5 0x1c15962 0x1c46bb6 0x1c45f44 0x1c45e1b 0x1bfa7e3 0x1bfa668 0x2565c 0x233d 0x2265)
    libc++abi.dylib: terminate called throwing an exception
    (lldb) 
    thanks
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Set break points. Step through your code and find where that exception is being thrown.

    To set a breakpoint, click on the (numbered) column to the left of your code.

    Your code will pause execution when it hits that point. You can then use the rightward arrow button beneath your code to step through it and determine the exact line causing the issue. You can also inspect variables and determine why they're nil using the debugger.
     
  3. leconteconte thread starter macrumors newbie

    Joined:
    Feb 28, 2013
    #3
    Thanks for idea now i have this problem
    that is the method where my is the crach
    Code:
    -(NSMutableArray*)getElementValue{
        //NSString *nodeValue;
        NSString *nbreSess;
        NSString *idSession;
        NSString *nomAppli;
        NSString *nameMach;
        NSString *guidSession;
        NSMutableArray *results = [[NSMutableArray alloc]init];
        
        
        
        
        NSLog(@"Commandes=> %@",commandes);
        //
        for (NSMutableDictionary *val in commandes) {
           
            nbreSess = [val objectForKey:@"NbreSecEnCours"];
          
            nbreSess = [nbreSess stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            nbreSess = [nbreSess stringByReplacingOccurrencesOfString:@"\t" withString:@""];
            NSLog(@"Nbre sess :=> %@",nbreSess);
            [results addObject:nbreSess];
            
        }
         for (NSMutableDictionary *val in commandes) {
           nomAppli = [val objectForKey:@"NomAppli"];
           nomAppli = [nomAppli stringByReplacingOccurrencesOfString:@"\n" withString:@""];
           nomAppli = [nomAppli stringByReplacingOccurrencesOfString:@"\t" withString:@""];
            NSLog(@"NomAppli :=> %@",nomAppli);
           [results addObject:nomAppli];
    
            
            nameMach = [val objectForKey:@"NameMach"];
            nameMach = [nameMach stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            nameMach = [nameMach stringByReplacingOccurrencesOfString:@"\t" withString:@""];
             NSLog(@"Name Mach :=> %@",nameMach);
            [results addObject:nameMach];
            
            
            
            guidSession = [val objectForKey:@"GuidSession"];
            guidSession = [guidSession stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            guidSession = [guidSession stringByReplacingOccurrencesOfString:@"\t" withString:@""];
            NSLog(@"GuidSession :=> %@",guidSession);
            [results addObject:guidSession];
            
         
           
            
        }
        NSLog(@"Results=> %@",results);
        //return nodeValue;
      //  return commandes;
        return results;
    }
    
    

    i think that the problem is the manner that i use to get the data inside de dictionnary because the log give me :
    Code:
    2013-02-28 12:57:22.796 XMLTesteur-1.1[1836:11303] Nbre sess :=> 2
    2013-02-28 12:57:22.797 XMLTesteur-1.1[1836:11303] Nbre sess :=> (null)
    
    that is my dictionnary where i want to get the value inside :
    Code:
    Commandes=> (
            {
            NbreSecEnCours = 2;
        },
            {
            GuidSession = "12729c94-1bf6-49ac-beca-e3c99609aded";
            NameMach = T2037;
            NomAppli = Notepad;
        },
            {
            GuidSession = "12729c94-1bf6-49ac-beca-e3c99609aded";
            NameMach = T2037;
            NomAppli = nslookup;
        }
    )
    
    how can i do to get the different key and value inside the dictionnary ???
    "NbreSecEnCours", "GuidSession" etc...
     

Share This Page