从XML解析两个值

时间:2014-12-19 20:12:52

标签: ios objective-c xml nsstring

我将一些XML放入一个字符串中:

xmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://developer.trimet.org/ws/V1/arrivals/locIDs/4016/appID/8D8D196B90DA0E177306D3CE8"] encoding:NSUTF8StringEncoding error:nil];

这给了我:

<?xml version=“1.0”encoding=“UTF-8”?>
<resultSet xmlns="urn:trimet:arrivals" queryTime="1419018749836">
<location desc="SE Morrison & 14th" dir="Westbound" lat="45.5172536692239" lng="-122.651457848479" locid="4016"/>
<arrival block="1546" departed="true" dir="0" status="estimated" estimated="1419019001000" fullSign="15 Belmont/NW 23rd to NW Thurman St" piece="1" route="15" scheduled="1419018793000" shortSign="15 To Thurman" locid="4016" detour="false">
<blockPosition feet="3781" at="1419018725000" heading="270" lat="45.5164371" lng="-122.6373635">
<trip desc="Thurman & 27th" dir="0" route="15" tripNum="5085057" destDist="37224" pattern="22" progress="33443"/>
</blockPosition>
</arrival>

我只希望元素queryTime中的数字和估计存储到两个单独的变量中,在这种情况下:

1419018749836

1419019001000

什么会解析字符串给我这两个数字并将它们分成两个独立的变量?谢谢。

2 个答案:

答案 0 :(得分:0)

由于您在XML响应中获得了多个对象,因此NSXMLParser解决方案可能会运行良好。

NSXMLParser *xml = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://developer.trimet.org/ws/V1/arrivals/locIDs/4016/appID/8D8D196B90DA0E177306D3CE8"]];

xml.delegate = self;
[xml parse]; 

然后实现委托方法以获取所需的值:

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {
    if ([elementName isEqualToString:@"resultSet"]) {
        NSLog(@"queryTime: %@", [attributeDict valueForKey:@"queryTime"]);
    }
    if ([elementName isEqualToString:@"arrival"]) {
        NSLog(@"estimated: %@", [attributeDict valueForKey:@"estimated"]);
    }
}

由于该服务当前返回两个“到达”,因此输出为:

2014-12-19 13:59:44.839 test[85905:4628499] queryTime: 1419022785305
2014-12-19 13:59:44.840 test[85905:4628499] estimated: 1419023316000
2014-12-19 13:59:44.840 test[85905:4628499] estimated: 1419024525000

答案 1 :(得分:-2)

实现这一目标有两种方法:

  1. 使用NSXMLParser
  2. 使用NSScanner
  3. 如果属性不在多个节点中重复,并且您不需要解析多个值,则可以使用 NSScanner

    NSString *replacement = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://developer.trimet.org/ws/V1/arrivals/locIDs/4016/appID/8D8D196B90DA0E177306D3CE8"] encoding:NSUTF8StringEncoding error:nil];
    NSScanner* scanners = [NSScanner scannerWithString:replacement];
    [scanners setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"\""]];
    [scanners scanUpToString:@"queryTime=" intoString:nil];
    [scanners scanUpToString:@"\"" intoString:nil];
    if (![scanners isAtEnd])
    {
        NSString *queryTime     = nil;
        NSString *estimated = nil;
        [scanners scanUpToString:@"\">" intoString:&queryTime];
        if (queryTime)
        {
            NSLog(@"Query : %@",queryTime);
        }
    
        [scanners scanUpToString:@"estimated=" intoString:&estimated];
        [scanners scanUpToString:@"\"" intoString:&estimated];
        [scanners scanUpToString:@"\"" intoString:&estimated];
        if (estimated)
        {
            NSLog(@"Estimated : %@",estimated);
        }
    
    }
    

    如果需要解析各种值并且属性可以存在于多个节点中,则需要使用NSXMLParser。请查看此document以供参考。