IOS:如何在一个XML列表中读取特定数据

时间:2013-05-02 10:47:13

标签: ios xml parsing

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

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

但是,如果我想从列表中提取数据,e.x。我想从&lt;&gt; id&gt;获取lat和lon,我该如何处理?

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

如果我使用而不是&lt;&gt; id&gt;这是很奇怪的,它会消失。所以,我必须使用这个丑陋的符号。

提前谢谢!

1 个答案:

答案 0 :(得分: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;
}

从params找到lat和lon

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"];