1

XML结构中我有:

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

它适用于<mytag>something</mytag>. 如何获取标签属性?

4

3 回答 3

5

attributeDictionary

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

包含它们。打个比方[atrributeDict objectForKey:@"fontColor"]_

于 2012-04-06T13:04:55.570 回答
2

NSXMLParserDelegate 协议有一个– parser:foundAttributeDeclarationWithName:forElement:type:defaultValue:方法,解析器使用该方法告诉委托每个属性。在您的委托中实现该方法,您将获得属性。

于 2012-04-06T13:13:43.183 回答
1

使用 xpathQuery.. 它是 XML 解析最简单、最快的解决方案.. 这将解决您的问题.. 这是链接..

http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html

顺便说一句:对于“查询”部分,您需要将“//”作为根元素,“//root//item”作为内部层次结构。

于 2012-04-06T12:46:24.193 回答