【问题标题】:iphone using NSXML parser how to parse the attributesiphone使用NSXML解析器如何解析属性
【发布时间】:2011-08-10 08:42:15
【问题描述】:

我想知道如何解析下面xml文件中的属性

<ROOT_ELEMENT><RESPONSE READ_TAG="LEVEL_LIST" RESULT="" TEXT=""/><USER USER_NAME="newadmin01" TOKEN_ID="0.766003221016982" FULL_NAME="newadmin01, newadmin01"/><DATETIME UNFORMATTED_TEXT="Aug 10 2011 12:25PM" FORMATTED_TEXT="10 Aug 12:25"/><BREADCRUMB/><LEVEL_LIST><LEVEL ID="4519" NAME="Mega Mart" CHILD_EXISTS="Y" ADD_EDIT_PRIVILEGE="Y"/></LEVEL_LIST></ROOT_ELEMENT>

这是我的解析器代码

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    elemName = [[NSString alloc] initWithString:elementName];
    NSLog(@"element Name = %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {   
    if ([elemName isEqualToString:@"RESPONSE"]) {
        if (!currentValueString) {
            currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
        }
        [currentValueString appendString:string];
    }
    else if ([elemName isEqualToString:@"USER"]) {
        if (!currentValueString) {
            currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
        }
        [currentValueString appendString:string];
    }
    if ([elemName isEqualToString:@"DATETIME"]) {
        if (!currentValueString) {
            currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
        }
        [currentValueString appendString:string];
    }
    else if ([elemName isEqualToString:@"BREADCRUMB"]) {
        if (!currentValueString) {
            currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
        }
        [currentValueString appendString:string];
    }
    else if ([elemName isEqualToString:@"LEVEL"]) {
        if (!currentValueString) {
            currentValueString = [[NSMutableString alloc] initWithCapacity:1024];
        }
        [currentValueString appendString:string];
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if ([elemName isEqualToString:@"RESPONSE"]) {
        [tableDataArray addObject:currentValueString];

        [currentValueString release];
        currentValueString = nil;

        [elemName release];
        elemName = nil;

    }   
    else if ([elemName isEqualToString:@"USER"]) {
        [tableDataArray addObject:currentValueString];

        [currentValueString release];
        currentValueString = nil;

        [elemName release];
        elemName = nil;

    }   
    else if ([elemName isEqualToString:@"DATETIME"]) {
        [tableDataArray addObject:currentValueString];

        [currentValueString release];
        currentValueString = nil;

        [elemName release];
        elemName = nil;

    }   
    else if ([elemName isEqualToString:@"BREADCRUMB"]) {
        [tableDataArray addObject:currentValueString];

        [currentValueString release];
        currentValueString = nil;

        [elemName release];
        elemName = nil;

    }   
    else if ([elemName isEqualToString:@"LEVEL"]) {
        [tableDataArray addObject:currentValueString];

        [currentValueString release];
        currentValueString = nil;

        [elemName release];
        elemName = nil;
    }   

}

【问题讨论】:

    标签: iphone


    【解决方案1】:

    查看 NSXMLParserDelegate 的消息声明

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

    它有一个你当前解析的元素的attributeDict。

    遍历键值对并根据需要处理它们。

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
        elemName = [[NSString alloc] initWithString:elementName];
        NSLog(@"element Name = %@", elementName);
        NSEnumerator *keyEnumerator = attributeDict.keyEnumerator()
    
    
        // for each element the parser encounters:
        // create a new dictionary to hold the attributes
        NSMutableDictionary *myAttributes = [[NSMutableDictionary alloc] init];
    
        // go through each attribute for the elementName tag
        while ( (id aKey = [keyEnumerator nextObject]) )
             // and store the attribute to the dictionary by copying over the values
             [myAttributes setObject:[attributesDict objectForKey:aKey] forKey:aKey];
    
        // after we caught all attributes and copied them into our own data structure
        // put them into an instance dicionary to get a structure which holds 
        // all attributes for any element tag we encounter 
        // accessible by the elementName as key
        [self.attributesByElementDictionary setObject:myAttributes forKey:elementName];
        [myAttributes release];
    }
    

    实际上,这将为 - 示例生成一个字典! - 响应标签: 字典看起来像:

    {
        "READ_TAG": "LEVEL_LIST",
        "RESULT": "",
        "TEXT": ""
    }
    

    对于更通用的解决方案,您不会使用平面字典,而是形成某种树结构。这需要更多的工作和更健壮的 - 但这个小例子应该让您了解从标签中提取属性并在之后处理它们

    【讨论】:

    • 您能否通过示例详细说明您的答案,例如使用 NSXML 解析器解析以下 xml 文件。
    猜你喜欢
    • 2011-10-23
    • 2012-07-14
    • 1970-01-01
    • 2011-12-24
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多