【问题标题】:How to display Xpath on the iPhone如何在 iPhone 上显示 Xpath
【发布时间】:2012-02-10 01:13:03
【问题描述】:

我正在尝试使用 iPhone 上的 Xpath 从here 提取天气信息。到目前为止,它解析了所有数据,但我坚持如何提取内容并将其显示在表格中。 这是我目前所拥有的:

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  

如果有人需要查看到目前为止的输出,请告诉我。

谢谢, 安德鲁

【问题讨论】:

  • 在 Google 上查看“UITableView 教程”,例如google.com/…
  • 不是显示我看不懂的表格,而是组织数据以便我可以显示它。不过谢谢
  • 我想我不明白。你想展示什么数据?如果您可以将数据放入数组中,那么您可以将其放入表中。看起来您已经在 elements 中拥有了该数组,只需循环遍历您的数组即可生成表格单元格。

标签: iphone objective-c xpath ios5 html-parsing


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 2012-10-04
    • 2011-04-07
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多