2013-04-01 30 views
0

我有以下问题:我正在构建一个应用程序,它是一个电视指南。我正在解析互联网上xml文件中的频道列表。这是我的代码:如何根据Xcode中的当前日期解析不同的文件?

-(void)loadListing 
{ 
    NSURL *urlListing = [NSURL URLWithString:@"http://pik.bg/TV/bnt1/29.03.2013.xml"]; 

    NSData *webDataListing = [NSData dataWithContentsOfURL:urlListing]; 

    NSString *xPathQueryListing = @"//elem/title"; 

    TFHpple *parserListing = [TFHpple hppleWithXMLData:webDataListing]; 

    NSArray *arrayListing = [parserListing searchWithXPathQuery:xPathQueryListing]; 

    NSMutableArray *newArrayListing = [[NSMutableArray alloc] initWithCapacity:0]; 

    for (TFHppleElement *element in arrayListing) 
    { 
     Listing *shows = [[Listing alloc] init]; 
     [newArrayListing addObject:shows]; 
     shows.broadcast = [[element firstChild] content]; 
    } 

    _shows = newArrayListing; 
    [self.tableView reloadData]; 
} 

看第一行 - 我的文件的名称是/.../01.04.2013.xml 明天的文件将/.../02.04.2013.xml等 如何根据当前日期来解析不同的文件?像这样:今天解析/.../01.04.2013,明天将解析/.../02.04.2013等?提前致谢!

+1

找出当前的日期,然后传递当前日期urlString串..... –

回答

1
  1. 首先,使用URL中使用的相同格式获取今天的日期。 (你有独立的datemonthyear元器件起到)

  2. 然后,NSStringNSString *strToDay = [NSString stringWithFormat:@http://pik.bg/TV/bnt1/%@.xml",strToDay];

  3. 使用字符串转换该日期为NSString对象

  4. 形式进入NSURL,喜欢;如果您的网址包含由您指定的日期格式 NSURL *urlListing = [NSURL URLWithString:strToDay];

注意此解决方案才有效。

+0

谢谢你,我的朋友,这解决了我的问题!这并不像我想的那么难。 – scourGINHO

+1

很高兴我能帮忙:) – viral

0

您可以使用NSDateFormatter配置的属性来生成适当格式的字符串。使用[NSDate date]返回的NSDate实例获取今天的日期,并使用格式化程序生成字符串。最后,将日期的字符串表示插入到URL字符串中,并从中构建一个NSURL

// Assuming the TV schedule is derived from the Gregorian calendar 
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

// Use the user's time zone 
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone]; 

// Instantiate a date formatter, and set the calendar and time zone appropriately 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setCalendar:gregorianCalendar]; 
[dateFormatter setTimeZone:localTimeZone]; 

// set the date format. Handy reference here: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns 
[dateFormatter setDateFormat:@"dd.MM.yyyy"]; 

// [NSDate date] returns a date corresponding to 'right now'. 
// Since we want to load the schedule for today, use this date. 
// stringFromDate: converts the date into the format we have specified 
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]]; 

// insert the date string into the URL string and build the URL 
NSString *URLString = [NSString stringWithFormat:@"http://pik.bg/TV/bnt1/%@.xml", dateString]; 
NSURL *URL = [NSURL URLWithString:URLString]; 

NSLog(@"URL = %@", URL); 
相关问题