问题描述:

I must read an XML file and read the id attribute within the XML file, but the dictionary out null. The xml is:

 <?xml version='1.0' encoding='UTF-8'?>

<root>

<event id="2"></event>

</root>

My code:

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

attributes:(NSDictionary *)attributeDict {

NSLog(@"elementName is %@", elementName);

// elementName is root

NSLog(@"AttributeDict is %@", attributeDict);

//AttributeDict is { } why?

//I must extract "id" in event but does not entry

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

NSLog(@" %@", elementName);

}

}

网友答案:

You need to do like:

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

}

The element root don't have any attributes, that's why the attributeDict displays as empty, when elementName is root.

相关阅读:
Top