How to use NSXMLParser to parse parent-child elements that have the same name

后端 未结 3 1028
时光取名叫无心
时光取名叫无心 2020-11-29 00:17

Does anyone have some idea how to parse the following xml using event-driven model NSXMLParser class?


   Main 
  

        
3条回答
  •  借酒劲吻你
    2020-11-29 00:31

    A very easier way to solve it than the accepted answer, for short XML files:

    XML example:

    
        
                50058
                -0.16
        
        
                3.4610
                +0.29
        
        
                3.7673
                -0.25
        
        04/08/15 - 18:14
    
    

    Implement a counter:

    @implementation className{
        int parserCounter;
    }
    
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        parserCounter++;
    
        NSLog(@"%d: %@",contadorParser,string);
    
        if (parserCounter == 3) doAnythingFor3;
        if (parserCounter == 5) doAnythingFor5;
        if (parserCounter == 9) doAnythingFor9;
        if (parserCounter == 11) doAnythingFor11;
        if (parserCounter == 15) doAnythingFor15;
        if (parserCounter == 17) doAnythingFor17;
        if (parserCounter == 20) doAnythingFor20;
    
    }
    

    How will i know witch number is witch node ? By seeing the log. It will be something like:

    2015-08-04 23:36:08.070 Tesouro Direto[7252:1554402] 1:     
    2015-08-04 23:36:08.070 Tesouro Direto[7252:1554402] 2:     
    2015-08-04 23:36:08.071 Tesouro Direto[7252:1554402] 3:     50058
    2015-08-04 23:36:08.105 Tesouro Direto[7252:1554402] 4:     
    2015-08-04 23:36:08.105 Tesouro Direto[7252:1554402] 5:     -0.16
    2015-08-04 23:36:08.131 Tesouro Direto[7252:1554402] 6:     
    2015-08-04 23:36:08.132 Tesouro Direto[7252:1554402] 7:     
    2015-08-04 23:36:08.132 Tesouro Direto[7252:1554402] 8:     
    2015-08-04 23:36:08.132 Tesouro Direto[7252:1554402] 9:     3.4610
    2015-08-04 23:36:08.156 Tesouro Direto[7252:1554402] 10:    
    2015-08-04 23:36:08.156 Tesouro Direto[7252:1554402] 11:    +0.29   
    2015-08-04 23:36:08.180 Tesouro Direto[7252:1554402] 12:    
    2015-08-04 23:36:08.180 Tesouro Direto[7252:1554402] 13:    
    2015-08-04 23:36:08.180 Tesouro Direto[7252:1554402] 14:    
    2015-08-04 23:36:08.180 Tesouro Direto[7252:1554402] 15:    3.7673
    2015-08-04 23:36:08.203 Tesouro Direto[7252:1554402] 16:    
    2015-08-04 23:36:08.203 Tesouro Direto[7252:1554402] 17:    -0.25
    2015-08-04 23:36:08.226 Tesouro Direto[7252:1554402] 18:    
    2015-08-04 23:36:08.227 Tesouro Direto[7252:1554402] 19:    
    2015-08-04 23:36:08.227 Tesouro Direto[7252:1554402] 20:    04/08/15   - 18:14
    2015-08-04 23:36:08.227 Tesouro Direto[7252:1554402] 21: 
    

    The log will show you witch number is each node.

提交回复
热议问题