IOS:具有多个节点的 NSMXLParser

标签 ios xcode nsxmlparser feed

我有一个 xml 文件:

<data>
    <first>
        <city>
            city
        </city>
        <people>
            400
        </people>
    </first>
    <size>
        <width>
            340
        </width>
        <height>
            120
        </height>
    </size>
    <description>
        <temp>
            sunny
        </temp>
        <people>
            45
        </people>
    </description>
    <description>
        <temp>
            cloudy
        </temp>
        <people>
                90
        </people>
    </description>

我用来解析这段代码:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            

    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"first"]) {
        firstType = [[NSMutableDictionary alloc] init];
        currentCity = [[NSMutableString alloc] init];
        currentPeople = [[NSMutableString alloc] init];
    }

    if ([elementName isEqualToString:@"size"]){
        currentSize = [[NSMutableDictionary alloc] init];
        width = [[NSMutableString alloc]init];
        height = [[NSMutableString alloc]init];
    }

    if ([elementName isEqualToString:@"description"]){
        desc1 = [[NSMutableDictionary alloc] init];
        temp1 = [[NSMutableString alloc]init];
        people1 = [[NSMutableString alloc]init];
    }

    if ([elementName isEqualToString:@"description"]){
        desc2 = [[NSMutableDictionary alloc] init];
        temp2 = [[NSMutableString alloc]init];
        people2 = [[NSMutableString alloc]init];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     

    if ([elementName isEqualToString:@"first"]) {

        [firstType setObject:currentType forKey:@"city"];
        [firstType setObject:currentQuery forKey:@"people"];
                [feed addObject:[firstType copy]];
    }
    if ([elementName isEqualToString:@"size"]){

        [currentSize setObject:tempC forKey:@"width"];
        [currentSize setObject:tempF forKey:@"height"];
        [feed addObject:[currentSize copy]];
    }
    if ([elementName isEqualToString:@"description"]){

        [desc1 setObject:temp1 forKey:@"temp1"];
        [desc1 setObject:people1 forKey:@"people1"];
        [feed addObject:[desc1 copy]];
    }

    if ([elementName isEqualToString:@"description"]){

        [desc2 setObject:temp1 forKey:@"temp2"];
        [desc2 setObject:people1 forKey:@"people2"];
        [feed addObject:[desc2 copy]];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    NSLog(@"found");
    if ([currentElement isEqualToString:@"city"]){
        [currentCity appendString:string];
    } 
    else if ([currentElement isEqualToString:@"people"]) {
        [currentPeople appendString:string];
    } 
    else if ([currentElement isEqualToString:@"width"]){
        [width appendString:string];
    }
    else if ([currentElement isEqualToString:@"height"]){
        [height appendString:string];
    }
    else if ([currentElement isEqualToString:@"temp"]){
        [temp1 appendString:string];
    }
    else if ([currentElement isEqualToString:@"temp"]){
        [temp2 appendString:string];
    }
    else if ([currentElement isEqualToString:@"people"]){
        [people1 appendString:string];
    }
    else if ([currentElement isEqualToString:@"people"]){
        [people2 appendString:string];
    }

}

- (void) parserDidEndDocument:(NSXMLParser *)parser {

    NSLog(@"feed:%@",feed);
}

nslog的结果是:

feed:(
        {
        city = city;
        people = 4004590;
    },
        {
        width = 340;
        height = 120;
    },
        {
        temp = sunny;
        people = "";
    },
        { ///???? here there is an empty space
    },
        {
        temp = cloudy;
        people = "";
    },
        {
    }
)

现在我不明白为什么desc 1和desc2的第一个字典之间有一个空格,我不知道“people”如何将people1和people2的结果放在一个字符串中

你能帮帮我吗?

最佳答案

我猜你的问题出在重复的代码块中,例如:

else if ([currentElement isEqualToString:@"temp"]){
    [temp1 appendString:string];
}
else if ([currentElement isEqualToString:@"temp"]){
    [temp2 appendString:string];
}

在这种情况下,您的第一部分代码将执行两次,第二部分永远不会执行。

还要检查您代码的其他部分,这个问题有几个障碍。

关于IOS:具有多个节点的 NSMXLParser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9968179/

相关文章:

iphone - 从网络服务多次获得相同的响应

ios - UIImagePickerController 的状态栏样式错误

ios - Xcode 6 创建 Storyboard警告

iphone - 子类崩溃 - iOS

c++ - 如何在 Xcode 中链接动态库?

objective-c - NSXMLParser 创建节点和 NSMutableArray

ios - React native 性能模拟器与真实设备

ios - Nativescript:iOS 回调上的 Zeroconf/Bonjour 发现不起作用

iphone - AppDelegate 导航错误

ios - NSXMLParserErrorDomain 111