2011-08-10 59 views
2

我想知道如何在下面的XML文件iphone使用NSXML解析器如何解析屬性

<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; 
    } 

}

回答

0

看看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": "" 
} 

對於您不會使用平板字典而是形成某種樹結構的更通用的解決方案。這是一點點的工作和更強大的 - 但這個小例子應該給你的想法來提取標籤的屬性並對其進行處理後

+0

你能否解釋與例子來支持你的答案,就像使用NSXML解析下面的XML文件解析器。 dchiphone