2012-10-31 50 views
-1

當我嘗試解析使用的NSXMLParser我couldnot解析所有元素的XML文件.. XML響應如下所示:NSXMLParser無法在iPhone中添加所有對象?

<List> 
    <Details> 
      <AId>1</AId> 
      <Date>10/31/2012</Date> 
      <Time>11:00 AM</Time> 
     <User> 
      <Employee> 
       <Name>Rosy</Name> 
       </Employee> 
       <Company> 
        <Customer> 
        <CustName>Williams</CustName> 
        <CustPhone>1232322</CustPhone> 
       </Customer> 
      </Company> 
     </User> 
    </Details> 
<Details> 
      <AId>2</AId> 
      <Date>10/31/2012</Date> 
      <Time>11:30 AM</Time> 
     <User> 
      <Employee> 
       <Name>Mary</Name> 
       </Employee> 
       <Company> 
        <Customer> 
        <CustName>Christopher</CustName> 
        <CustPhone>123233323</CustPhone> 
       </Customer> 
      </Company> 
     </User> 
    </Details> 
</List> 

我怎樣才能得到所有的所有的XML標籤elemnts?

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


if([elementName isEqualToString:@"AList"]) 
{ 
    arr=[[NSMutableArray alloc] init]; 



} 
    if([elementName isEqualToString:@"Details"]||[elementName isEqualToString:@"User"]) 
    { 
     drr=[[NSMutableDictionary alloc] init]; 
    } 

srr=[[NSMutableString alloc] init]; 

} 


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


    [srr appendString:string]; 
} 


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

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

     [drr setObject:srr forKey:@"AId"]; 
     [srr release],srr=nil; 
     return; 
    } 

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

     [drr setObject:srr forKey:@"Date"]; 
     [srr release],srr=nil; 
     return;  
    } 
    if([elementName isEqualToString:@"Time"]) 
    { 

     [drr setObject:srr forKey:@"Time"]; 
     [srr release],srr=nil; 
    return;  
    } 
    if([elementName isEqualToString:@"Name"]) 
    { 

     [drr setObject:srr forKey:@"Name"]; 
     [srr release],srr=nil; 
     return; 
    } 
     if([elementName isEqualToString:@"CustName"]) 
    { 
     [drr setObject:srr forKey:@"CustName"]; 
     [srr release],srr=nil; 


     return; 
    } 

    if([elementName isEqualToString:@"CustPhone"]) 
    { 
     [arr addObject:drr]; 

    }  



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

     [drr release]; 


    } 

    [srr release],srr=nil; 

} 

我怎樣才能得到它..... couldnnt明白的地方我錯了....

+0

使用正確的XML解析,例如內置於libxml2中的DOM解析器。它更快,更輕量,唯一的缺點是它是一個基於C的庫。 –

回答

0

您使用DidEndElement方法下面的代碼它會幫助你...

-(void)parser:(NSXMLParser*)parser 
didEndElement:(NSString*)elementName 
namespaceURI:(NSString*)namespaceURI 
qualifiedName:(NSString*)qualifiedName 
{ 
    if ([elementName isEqualToString:@"list"]) { 
     return; 
    } 
else if ([elementName isEqualToString:@"Details"]){ 
[yourarray addObject:yourDictionary]; 
} 
    else{ 
     if (currentElementValue.length>0) { 
      [yourDictionary setObject:currentElementValue forKey:elementName]; 
      currentElementValue = nil; 
      NSLog(@"this is the offer details---%@",[yourDictionary description]); 
     } 
    } 
} 

-(void)parser:(NSXMLParser*)parser 
foundCharacters:(NSString*)string 
{ 
    currentElementValue=[NSString stringWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]]; 
} 
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock 
{ 
    currentElementValue = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding]; 
} 
+0

你聲明瞭什麼是currentElementValue? – 07405

+0

NSMutableString * currentElementValue; –

+0

請參閱我的編輯代碼 –