2012-03-17 4 views
1

이미 많이 검색했지만 튜토리얼과 샘플 코드는 도움이되지 않았습니다. 난 항상 한 결과와 동일합니다 아주 간단한 XML 데이터를 구문 분석을 시도하고, XML은 코드가 우는 소리입니다 : 그래서서버에서 XML 파일을 구문 분석하는 방법은 무엇입니까?

<Books> 
    <Book id="1"> 
     <title>USERS ALREADY EXISTS</title> 
    </Book> 
</Books> 

, 내가 NSXMLParser 또는 다른 방법을 사용하여 이러한 파일을 구문 분석 할 수있는 방법 알고있다?

+0

(http://whathaveyoutried.com)는 XML을 구문 분석 애플의 API가 [잘 문서화 (https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual /XMLParsing/XMLParsing.html) 및 예시 [샘플 코드]를 갖는다 (https://developer.apple.com/library/ios/samplecode/SeismicXML/). 이것들은 당신에게 당신의 문제를 어떻게 해결할 것인지 정확하게 알려주지는 않지만, 거기에서 당신의 길을 찾을 수있을만큼 API에 익숙해 지거나 더 구체적인 질문을해야합니다. – rickster

+0

Ishu의 링크로 이동할 수도 있습니다. 내 메서드는 문자열 함수와 함께 재생하고 이해하기 쉽습니다. 당신이 쉽게 찾을 수있는 것을 채택하십시오. 그러나 당신은 구현하기 전에 그것을 잘 이해하고 있습니다. 행운을 빕니다. –

답변

2

이 잘 나는 XML을 다른 사람보다 몇 가지 다른 방법을 분석하고 내가 정말 어떤 기술 모른다 솔직하게하지만 난 당신이 나를 위해 잘 작동 보장과 나는 많은 프로젝트에 성공적으로 implemeted했다. 좀 profile

이에서 트윗을로드 할 경우 내 코드에서보세요 것은 내가 파서 전화를 걸 수있는 기능입니다.

-(void)loadtweet 
{ 
@try 
{ 
    NSString *urlString = [NSString stringWithFormat:@"https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=SrBachchan&count=5"]; 

    NSLog(@"fetching data from--------> : %@",urlString); 

    NSString* escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 

    NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:escapedUrlString]]; 

    NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:request1 delegate:self]; 
    if(con) 
     truckData=[[NSMutableData data]retain]; 
} 

@catch (NSException *exception) 
{ 
    UIAlertView *v = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Please Try Again Later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [v show]; 
    [v release]; 
} 

} 

그리고 이것들은 NSURLConnection 위임 방법은 다음과 같습니다

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

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

{ 
    [tweets removeAllObjects]; 
@try 
{ 
    // [app.trucks removeAllObjects]; 
    NSString *thexml=[[NSString alloc] initWithBytes:[truckData mutableBytes] length:[truckData length] encoding:NSUTF8StringEncoding]; 

    NSArray *array=[thexml componentsSeparatedByString:@"<status>"]; 
    NSLog(@"%d",[array count]); 

    for(int i=1;i<[array count];i++) 
    { 
     NSString *str=[array objectAtIndex:i]; 
     NSArray *arr1=[str componentsSeparatedByString:@"<text>"]; 
     NSString *data=[arr1 objectAtIndex:1]; 
     NSRange ranfrom=[data rangeOfString:@"</text>"]; 
     // nt.truckName=[data substringToIndex:ranfrom.location]; 
     [tweets addObject:[data substringToIndex:ranfrom.location]]; 
    } 
} 

@catch (NSException *exception) 
{ 
    UIAlertView *v = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Please Try Again Later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [v show]; 
    [v release]; 
} 

} 

내가 태그를 분리하기 위해 몇 가지 문자열 함수를 사용하고 배열의 값을 저장했다. [당신이 시도 무엇?]

+0

그냥 코드를 확인하려면 truckData가 NSMutableArray라고 가정합니다. 그리고 짹짹도 NSArray입니까? – Mateus

+0

@MateusNunes : 예. 트윗은 모든 트윗을 개최하고 .. :-) .. jQuery과에 그들에게 보여 NSLog 퍼팅 시도하고 당신이이 문자열 함수가 사용되는 방법을 알고 얻을 것이다 출력이 표시됩니다) 나는 내 대답을 편집하고 몇 가지 의견을 넣어하는있는 NSMutableArray입니다 .. :-) –

관련 문제