2012-04-06 39 views
1

XML結構,我有:如何在Objective-C中使用NSXMLParser獲取標籤參數?

<font fontsize="10" fontcolor="#000000" fontface="file.ttf"/> 

我如何獲得字號,顏色和臉部使用NSXMLParser? 我當然有標準的實施

-(id)init 
{ 
    self = [super init]; 

    parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myxmlfile" ofType:@"xml"]]]; 

    [parser setDelegate:self]; 
    [parser parse]; 

    return self; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
    NSLog(@"Started Element %@", elementName); 
    element = [NSMutableString string]; 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    NSLog(@"Found an element named: %@ with a value of: %@", elementName, element); 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    if (element == nil) 
     element = [[NSMutableString alloc] init]; 
    [element appendString:string]; 
} 

和它的作品beautifly的<mytag>something</mytag>。如何獲取標籤屬性?

回答

5

attributeDictionary

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

包含它們。例如,請致電[atrributeDict objectForKey:@"fontColor"]

+0

謝謝!完美的作品:) – Kuba 2012-04-06 13:21:53

+0

很高興幫助你:) – 2012-04-06 13:22:55

相關問題