Objective-C:如何读取XML中的属性值

时间:2013-11-18 06:01:53

标签: ios iphone objective-c xml

嗨朋友们对iphone app开发新手。实际上我必须解析XML文件并将其显示在tableviewcontroller中。我正在使用故事板。我已成功解析Xml并解析数据并显示它。但我无法读取此属性<Event count="-1">我已读取此值并必须在标签栏上显示我必须将徽章值设置为-1 请帮帮我怎么做。在此先感谢..这是我的Xml文件

<boards>
<board count="-1">
<newboard>
<id>9</id>
<invitationdet>qweq</invitationdet>
<readflag>1</readflag>
<responseflag>1</responseflag>
</newboard>
<newboard>
<id>8</id>
<invitationdet>asd</invitationdet>
<readflag>1</readflag>
<responseflag>1</responseflag>
</newboard>
</board>
<back count="0">
<backboard>
<id>2</id>
<feedbackdet>Meeting with Clients</feedbackdet>
<readflag>1</readflag>
<responseflag>0</responseflag>
</backboard>
<backboard>
<id>1</id>
<feedbackdet>Board Meeting with employees</feedbackdet>
<readflag>1</readflag>
<responseflag>0</responseflag>
</backboard>
</back>
<letter count="3">
<newletter>
<id>4</id>
<letterdet>NewsLetter for the month of October 2013</letterdet>
<readflag>1</readflag>
</newletter>
<newletter>
<id>3</id>
<letterdet>Newsletter for the month of September 2013</letterdet>
<readflag>0</readflag>
</newletter>
<newletter>
<id>2</id>
<letterdet>Newsletter for the month of August 2013</letterdet>
<readflag>0</readflag>
</newletter>
<newletter>
<id>1</id>
<letterdet>Newsletter For the month of July 2013</letterdet>
<readflag>0</readflag>
</newletter>
</letter>
</boards>

这是我的XMLParser文件

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"boards"]) {
        rssOutputData  = [[NSMutableArray alloc] init];
    }
    if([elementName isEqualToString:@"board"]) {
        //Initialize the array.

        aEvent.count = [attributeDict objectForKey:@"count"];
         NSLog(@"Reading count value :%@", aEvent.count);
    }
    else if([elementName isEqualToString:@"newboard"]) {
        //Initialize the book.
        aEvent = [[Eventlist alloc] init];
        //Extract the attribute here.
        aEvent.id = [[attributeDict objectForKey:@"userid"] integerValue];
        NSLog(@"Reading id value :%d", aEvent.id);
    }
    NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if(!nodecontent)
        nodecontent = [[NSMutableString alloc] initWithString:string];else
            [nodecontent appendString:string];
    NSLog(@"Processing Value: %@", nodecontent);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if([elementName isEqualToString:@"boards"])
        return;
    if([elementName isEqualToString:@"board"])
        return;
    if([elementName isEqualToString:@"newboard"]) {
        [rssOutputData addObject:aEvent];
        [aEvent release];
        aEvent= nil;
        }
    else
        [aEvent setValue:nodecontent forKey:elementName];
    [nodecontent release];
    nodecontent = nil;

}

我可以正确读取数据,但我无法读取属性值请帮助我的朋友

2 个答案:

答案 0 :(得分:-1)

在下面的方法中你注意到,有一个参数attributeDict,它包含了标签的所有属性

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

您可以按照上面的代码中的定义阅读它。只记录整个attributeDict,您就会得到答案。 - 祝你好运

答案 1 :(得分:-1)

尝试以下

 - (void)parser:(NSXMLParser *)parser
    didStartElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI
     qualifiedName:(NSString *)qualifiedName
        attributes:(NSDictionary *)attributeDict
    {
        // just do this for item elements
        if(![elementName isEqual:@"letter"])
            return;

        // then you just need to grab each of your attributes
        int count  = [attributeDict objectForKey:@"count"];


    }