0

当我在 iPhone 应用程序中使用 NSXMLParser 解析 XML 时,我知道如何在这样的场景中执行此操作:

> <title>L3178 : Freiensteinau Richtung Grebenhain</title> // From XML

但是,如果我想从列表中提取数据,例如我想从 <>id> 中获取 lat 和 lon,我应该如何处理呢?

<>id> http://www.freiefahrt.info/?id=468B0243-E15C-4580-9AD2 14D8CF692999&lon=9.3495&lat=50.49465&country=DE&filter=0&expires=2013-12-20T03:13:00 <>/id>

如果我用 <>id> 代替,很奇怪,它会消失。所以,我必须使用这个丑陋的符号。

先感谢您!

4

1 回答 1

0

创建一个从 urlString 中提取参数的方法

- (NSDictionary *)paramsFromURLString:(NSString *)urlString
{
    NSRange range = [urlString rangeOfString:@"?"];
    NSString *subString = [urlString substringFromIndex:range.location+range.length];

    NSArray *components = [subString componentsSeparatedByString:@"&"];

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    for (NSString *string in components) {
        NSRange subRange = [string rangeOfString:@"="];
        NSString *key = [string substringToIndex:subRange.location];
        NSString *value = [string substringFromIndex:subRange.location+subRange.length];

        [params setObject:value forKey:key];

    }
    return params;
}

从参数中找到纬度和经度

NSString *urlString = @"http://www.freiefahrt.info/?id=468B0243-E15C-4580-9AD2 14D8CF692999&lon=9.3495&lat=50.49465&country=DE&filter=0&expires=2013-12-20T03:13:00";
NSDictionary *params = [self paramsFromURLString:urlString];

NSString *latitude = params[@"lat"];
NSString *longitude = params[@"lon"];
于 2013-05-02T11:19:53.713 回答