如何在iPhone上显示Xpath

发布于 2024-12-27 02:24:26 字数 1104 浏览 1 评论 0 原文

我正在尝试从 此处。到目前为止,它解析了所有数据,但我一直困惑于如何提取内容并将其显示在表格中。 这就是我到目前为止所得到的:

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  

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

谢谢, 安德鲁

I'm trying to extract the weather information from here using Xpath on the iPhone. As of now it parses all the data but I'm stuck on how to extract the content and display it in a table.
This is what I have so far:

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  

If anybody needs to see what its outputting so far, let me know.

Thanks,
Andrew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2025-01-03 02:24:26

我也遇到了同样的问题,我已经到了你的地步,但我无处可去,但我最终实现了这段代码。希望它能有所帮助,要使其正常工作,仍然需要一点点,但要根据我开发的应用程序的性质,这就是我能为您提供的一切。它并不多,只是您真正需要的代码的实际实现。

#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;
}

I had the same issue I got to the point your at and didn't no where to go but I end up implementing this code. Hope it helps there is still little bits need to make it work correctly but do to the nature of the app I have developed this is all I can give you. its not much more its just the actual implementation into your code that you need really.

#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]];
        }
    }
}
}

You only need either the above code or below code unless you want both.

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文