2012-08-16 84 views
0
<NewDataSet> 
     <Table> 
     <CaseId>743</CaseId> 
     <PartyId>11100550</PartyId> 
     <CartId>18</CartId> 

     </Table> 
     <Table> 
     <CaseId>742</CaseId> 
     <PartyId>11100549</PartyId> 
     <CartId>1148</CartId> 
     <BusinessID>19</BusinessID> 

     </Table> 
    </NewDataSet> 


     NSData* data = [xmlResponseData dataUsingEncoding:NSUTF8StringEncoding]; 

    // NSXMLParser *XMLparser = [[NSXMLParser alloc] initWithData:data]; 
    // [XMLparser setDelegate:self]; 

     BOOL success; 

     if (XMLparser) // addressParser is an NSXMLParser instance variable 
      [XMLparser release]; 

     XMLparser = [[NSXMLParser alloc] initWithData:data]; 
     [XMLparser setDelegate:self]; 
     [XMLparser setShouldResolveExternalEntities:YES]; 

     success = [XMLparser parse]; // return value not used 
     // if not successful, delegate is informed of error 

     if(success) 
      NSLog(@"Sucess Parsed"); 
     else 
      NSLog(@"Error Error Error!!!"); 

    // NSLog(@"Parsed string : %@",valueForItem); 

    } 

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
     namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
     attributes:(NSDictionary *)attributeDict { 

     if([elementName isEqualToString:@"NewDataSet"]) { 
      caseID_List = [[NSMutableArray alloc]init]; 
     } 
     NSLog(@"Processing Element: %@", elementName); 

     if ([elementName isEqualToString:@"CaseId"]) { 

     } 

    } 

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

     if(!currentElementValue) 
      currentElementValue = [[NSMutableString alloc] initWithString:string]; 
     else 
      [currentElementValue appendString:string]; 

     NSLog(@"Processing Value: %@", currentElementValue); 


    } 

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

     if([elementName isEqualToString:@"NewDataSet"]) 
return; 
    } 

我只需要CaseId來存儲給定的XML。任何人都可以在這個簡單的解析中給我建議!如何解析簡單的xml

對我來說,它的循環!並獲取所有詳細信息

+0

爲什麼不使用GDataXML解析器..它易於使用和解析任何種類的XML .. – vishy 2012-08-16 08:58:37

+0

你能告訴我,如果你得到了你的問題的答案,因爲我在這裏處理同樣的問題 – ichanduu 2012-10-10 07:04:13

回答

2

使用下面的代碼.Where Table是NSObject類與CaseId,PartyId,CartId作爲此類中的一個屬性。如果您有xml URL,只需調用loadXMLByURL方法並使用URl.After解析後即可獲得TableArray中的每個對象都有具有上述屬性的Table對象。

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; 
    } 
} 

讓我知道你是否有任何疑問。

+0

嗨桑迪我嘗試使用你的代碼的東西,並操縱我的XML解析器它的工作! – kiran 2012-08-16 13:23:41