代码之家  ›  专栏  ›  技术社区  ›  Jeffrey Kern

分析cocoa中的nsxmlnode属性

  •  11
  • Jeffrey Kern  · 技术社区  · 15 年前

    给定以下XML文件:

        <?xml version="1.0" encoding="UTF-8"?>
    <application name="foo">
     <movie name="tc" english="tce.swf" chinese="tcc.swf" a="1" b="10" c="20" />
     <movie name="tl" english="tle.swf" chinese="tlc.swf" d="30" e="40" f="50" />
    </application>
    

    如何访问电影节点的属性(“英语”、“中文”、“名称”、“A”、“B”等)及其关联值?我目前在Cocoa中有能力遍历这些节点,但我对如何访问电影nsxmlnodes中的数据感到困惑。

    有没有一种方法可以将每个nsxmlnode中的所有值转储到哈希表中并以这种方式检索值?

    正在使用nsxmldocument和nsxmlnodes。

    2 回复  |  直到 15 年前
        1
  •  12
  •   Jeffrey Kern    15 年前

    对!我不知怎么地回答了自己的问题。

    在迭代XML文档时,不要将每个子节点指定为nsxmlnode,而是将其指定为nsxmlelement。然后,您可以使用attributeForName函数,该函数返回一个nsxmlnode,您可以使用StringValue on获取该属性的值。

    因为我不擅长解释,这里是我的注释代码。这可能更有意义。

    //make sure that the XML doc is valid
    if (xmlDoc != nil) {
                //get all of the children from the root node into an array
                NSArray *children = [[xmlDoc rootElement] children];
                int i, count = [children count];
    
                //loop through each child
                for (i=0; i < count; i++) {
                    NSXMLElement *child = [children objectAtIndex:i];
    
                        //check to see if the child node is of 'movie' type
                        if ([child.name isEqual:@"movie"]) {
                        {
                            NSXMLNode *movieName = [child attributeForName:@"name"];
                            NSString *movieValue = [movieName stringValue];
    
                            //verify that the value of 'name' attribute of the node equals the value we're looking for, which is 'tc'
                            if ([movieValue isEqual:@"tc"]) {
                            //do stuff here if name's value for the movie tag is tc.
                            }
                        }
                }   
       }
    
        2
  •  9
  •   dreamlax    15 年前

    有两种选择。如果你继续使用 NSXMLDocment 你有一个 NSXMLNode * 对于电影元素,可以执行以下操作:

    if ([movieNode kind] == NSXMLElementKind)
    {
        NSXMLElement *movieElement = (NSXMLElement *) movieNode;
        NSArray *attributes = [movieElement attributes];
    
        for (NSXMLNode *attribute in attributes)
        {
            NSLog (@"%@ = %@", [attribute name], [attribute stringValue]);
        }
    }
    

    否则,可以切换到使用 NSXMLParser 相反。这是一个事件驱动的解析器,当委托解析了元素(以及其他事情)时,它会通知委托。你追求的方法是 parser:didStartElement:namespaceURI:qualifiedName:attributes:

    - (void) loadXMLFile
    {
        NSXMLParser *parser = [NSXMLParser parserWithContentsOfURL:@"file:///Users/jkem/test.xml"];
        [parser setDelegate:self];
        [parser parse];
    }
    
    
    // ... later ...
    
    -      (void)         parser:(NSXMLParser *)parser
                 didStartElement:(NSString *)elementName
                    namespaceURI:(NSString *)namespaceURI
                   qualifiedName:(NSString *)qualifiedName
                      attributes:(NSDictionary *)attributeDict
    {
        if ([elementName isEqualToString:@"movie"])
        {
            NSLog (@"%@", [attributeDict objectForKey:@"a"]);
            NSLog (@"%d", [[attributeDict objectForKey:@"b"] intValue]);
        }
    }