2011-08-18 4 views
1

iPhone에서 내 View의 필드를 채우기 위해 두 개의 다른 XML에서 데이터를 읽으려고합니다.iPhone : 동일한보기에서 여러 XML에 액세스하는 방법

동일한보기에서 여러 XML을 읽을 수있는 방법이 있습니까? 나는 하나의 XML을 읽고 파싱 할 수있다.

덕분에 다른 문자열 요소를 구문 분석을 isParsingNextString 부울, 이름을 을 사용하여 NSXML 파서

-(void)connection:(NSURLConnection *)connection 
didReceiveResponse:(NSURLResponse *)response{ 
[webData setLength:0]; 
} 


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
[webData appendData:data];  
} 

-(void)connection:(NSURLConnection *)connection 
didFailWithError:(NSError *)error{NSLog(@"Connection Error"); 
[connection release]; 
[webData release];  
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
NSLog(@"Done, Received bytes: %d",[webData length]); 
NSString *theXML =[[NSString alloc] initWithBytes:[webData mutableBytes]length:[webData length]encoding:NSUTF8StringEncoding]; 
NSLog(@"XML value %@",theXML); 
     [theXML release]; 
     if (xmlParser) { 
       [xmlParser release]; 
     } 

     xmlParser = [[NSXMLParser alloc]initWithData:webData]; 
     [xmlParser setDelegate:self]; 
     [xmlParser setShouldResolveExternalEntities:YES]; 
     [xmlParser parse]; 
     [connection release]; 
     [webData release]; 
} 

- (void)parserDidStartDocument:(NSXMLParser *)parser{ 
     NSLog(@"found file and started parsing"); 

     //colorTypes = [[NSMutableArray alloc]init]; 
     propertyCategories = [[NSMutableArray alloc]init]; 
} 


-(void)parser:(NSXMLParser *)parser didStartElement:(NSString 
*)elementName namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName attributes:(NSDictionary 
*)attributeDict{ 
     if ([elementName isEqualToString:@"GetCommonCodeByCatgResult"]) { 
     } 

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

       aCategory =[[Category alloc]init]; 

       aCategory.CMCodeDesc = [attributeDict objectForKey:@"CMCodeDesc"]; 
       } 

} 

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

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


} 

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

     if ([elementName isEqualToString:@"GetCommonCodeByCatgResult"]) { 
     [itemCategoryList reloadAllComponents]; 
    //[colorList reloadAllComponents]; 
     return; 
     } 
if ([elementName isEqualToString:@"SvcCommonCode"]) { 

    [propertyCategories addObject:aCategory.CMCodeDesc]; 
//[colorTypes addObject: aCategory.CMCodeDesc]; 
    [aCategory release]; 
     aCategory =nil; 

} 

else 
[aCategory setValue:currentElementValue forKey:elementName]; 
[currentElementValue release]; 
currentElementValue = nil; 

} 
+0

어떤 XML 구문 분석기를 사용합니까? XML을 파싱하기 위해 어떤 코드를 사용합니까? 이 정보가 없으면 누군가가이 질문에 답할 가능성이 매우 낮습니다. –

+0

코드를 추가했습니다 – Katy

답변

1

를 사용

//

.

//in viewDidLoad 
isParsingNextString = NO; 

//put this inside your begin parsing method 
if (isParsingNextString == NO) { 
    //parse 1st string URL 
} 

if (isParsingNextString == YES) { 
    //parse 2nd string URL 
} 

//when your document finishes set the bool to yes  
//now restart your parser inside an if statement so you dont have a parsing loop 
if(isParsingNextString == NO) { 
    isParsingNextString = YES; 
    [self parseXML]; 
} 
+0

감사합니다. Louie 구현하려고합니다. – Katy

+0

Hey Louie, 두 번째 XML이 파싱되지 않고 응용 프로그램이 충돌합니다. 콘솔에 오류가 없습니다. 어떤 제안? – Katy

+0

파서를 너무 빨리 공개하지 말고 두 번째 문자열에 다른 요소 유형을 설명했는지 확인하십시오. – Louie

관련 문제