2012-01-13 4 views
2

iPhone에서 Xpath를 사용하여 here에서 날씨 정보를 추출하려고합니다. 지금은 모든 데이터를 파싱하지만 내용을 추출하여 테이블에 표시하는 방법에 주저하고 있습니다.iPhone에서 Xpath를 표시하는 방법

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[ @"http://aviationweather.gov/adds/metars/?station_ids=1234&std_trans=translated&chk_metars=on&hoursStr=most+recent+only&submitmet=Submit"stringByReplacingOccurrencesOfString:@"1234" withString:self.title]]]; 

TFHpple * doc  = [[TFHpple alloc] initWithHTMLData:data]; 
NSArray * elements = [doc searchWithXPathQuery:@"//table[1]//tr"]; 
NSLog(@"%@", elements); 
TFHppleElement * element = [elements objectAtIndex:0]; 
[element content];    // Tag's innerHTML 
[element tagName];    // "a" 
[element attributes];   // NSDictionary of href, class, id, etc. 
[element objectForKey:@"href"]; // Easy access to single attribute 

사람은 지금까지 출력을 확인해야하는 경우 알려 주시기 : 이것은 내가 지금까지 가지고있는 것입니다.

감사합니다, 앤드류

+1

"UITableView tutorial"(예 : http://www.google.com/search?client=safari&rls=en&q=uitableview+tutorial&ie=UTF-8&oe=UTF-8 –

+0

내가 이해하지 못하는 테이블을 표시하지 않고, 데이터를 정리하여 그것을 표시 할 수 있습니다. 고마워. – Boos1993

+0

나는 이해하지 못한다. 어떤 데이터를 보여주고 싶습니까? 데이터를 배열에 넣을 수 있다면 테이블에 넣을 수 있습니다. 그리고 이미'elements'에 배열이있는 것처럼 보입니다. 배열을 반복하여 테이블 셀을 만드는 것입니다. –

답변

1

가 나는 점에에있어 어디로 갈하지 않았다 같은 문제가 있었다 그러나 나는이 코드를 구현하는 끝낸다. 희망은 그것이 조금 제대로 작동하도록해야하지만 여전히 내가 개발 한 응용 프로그램의 성질에 도움이 필요합니다. 실제로 코드를 실제로 구현해야하는 것은 아닙니다.

#import "XPathQuery.h" 


NSMutableArray *weatherArray = [[NSMutableArray arrayWithArray:0]retain]; // Initilize the NSMutableArray can also be done with just an NSArray but you will have to change the populateArray method. 
NSString *xPathLookupQuery = @"//table[1]//tr"; // Path in xml 
nodes = PerformXMLXPathQuery(data, xPathLookupQuery); // Pass the data in that you need to search through 
[self populateArray:weatherArray fromNodes:nodes]; // To populate multiple values into array. 

session = [[self fetchContent:nodes] retain]; // To populate a single value and returns value. 

- (void)populateArray:(NSMutableArray *)array fromNodes:(NSArray *)nodes 
{ 
for (NSDictionary *node in nodes) { 
    for (id key in node) { 
     if ([key isEqualToString:@"nodeContent"]) { 
      [array addObject:[node objectForKey:key]]; 
     } 
    } 
} 
} 

둘 다 원한다면 위 코드 또는 아래 코드 만 있으면됩니다.

- (NSString *)fetchContent:(NSArray *)nodes 
{ 
NSString *result = @""; 
for (NSDictionary *node in nodes) { 
    for (id key in node) { 
     if([key isEqualToString:@"nodeContent"]) { 
      result = [node objectForKey:key]; 
     } 
    } 
} 
return result; 
} 
관련 문제