在NSXMLParser中读取节点的id和值

时间:2010-08-22 08:33:45

标签: iphone xml iphone-sdk-3.0

我有一个像这样的XML节点:

<State id="1"> AA </State>

如何读取值'AA'?我可以使用:

读取id值“1”
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict { <br>

        //Extract the attribute here.
        aState.stateId = [[attributeDict objectForKey:@"id"] integerValue];
        NSLog(@"Reading id value :%i", aState.stateId);
    }
    NSLog(@"Processing Element: %@", elementName);
}

但无法读取值

1 个答案:

答案 0 :(得分:0)

据我记忆,要做到这一点,你应该实现这个方法

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

整个代码如下所示:

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

    foundState = [elementName isEqualToString:@"State"];

    }
 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
      if (foundState) {
        [stringBuffer appendString:string];
      }
   }
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 

namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if (foundState) {
      NSLog(@"AA = %@", stringBuffer);
      foundState = NO;
    }
}