how to parse simple xml

后端 未结 1 1101
一生所求
一生所求 2020-12-12 07:33
    
      7431110055018

        
                      
相关标签:
1条回答
  • 2020-12-12 08:18

    Use the Below Code.Where Table is NSObject Class with the CaseId,PartyId,CartId as a properties in this class.If you have the xml url just called loadXMLByURL method with URl.After parsing you will get Each Object in TableArray which have the Table object with above properties.

    NSMutableString *currentNodeContent;
    NSXMLParser     *parser;
    Tweet           *currentTweet;
    bool            isStatus;
    
    -(id) loadXMLByURL:(NSString *)urlString
    {
        _tweets         = [[NSMutableArray alloc] init];
        NSURL *url      = [NSURL URLWithString:urlString];
        NSData  *data   = [[NSData alloc] initWithContentsOfURL:url];
        parser          = [[NSXMLParser alloc] initWithData:data];
        parser.delegate = self;
        [parser parse];
        return self;
    }
    
    - (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }
    
        - (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
        {
            if ([elementname isEqualToString:@"Table"]) 
            {
                currentTable = [Table alloc];
                    isStatus = YES;
            }
        }
    
    - (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
        if (isStatus) 
        {
            if ([elementname isEqualToString:@"CaseId"]) 
            {
                currentTable.CaseId = currentNodeContent;
            }
            if ([elementname isEqualToString:@"PartyId"]) 
            {
                currentTable.PartyId = currentNodeContent;
            }
            if ([elementname isEqualToString:@"CartId"]) 
            {
                currentTable.CartId = currentNodeContent;
            }
    
        }
        if ([elementname isEqualToString:@"Table"]) 
        {
            [self.tableArray addObject:currentTable];
            currentTable = nil;
            currentNodeContent = nil;
        }
    }
    

    Let me know if you have any doubt.

    0 讨论(0)
提交回复
热议问题