English 中文(简体)
出口 可可的贡献
原标题:Parsing NSXMLNode Attributes in Cocoa

鉴于以下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>

我怎么能够接触到MOVIE节点的属性(“english”、“chinese”、“name”、“a”、“b”)及其相关价值? 目前在科恰,我有能力揭穿这些节点,但我无法查阅MOVIENSXMLNodes的数据,因此损失了一米。

难道我可以把每一个国家情报和安全局的所有价值观都放在一个哈希姆,然后收回这些价值吗?

Am using NSXMLDocument and NSXMLNodes.

最佳回答

YES! 我回答了我自己的问题。

在通过XML文件进行检索时,不要将每个儿童节日定为NSXMLNode,而是将其指定为NSXMLElement。 然后,你可以利用属性。 “Name”功能返回了NSXMLNode,你可以为此利用SstringValue获取属性价值。

自2006年以来 我在解释事情时很坏,我在这里评论了我的法典。 这可能更有意义。

//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.
                        }
                    }
            }   
   }
问题回答

有两个选择。 如果你继续使用<条码>NSXMLDocment,并且有一部电影内容的,你可以这样做:

if ([movieNode kind] == NSXMLElementKind)
{
    NSXMLElement *movieElement = (NSXMLElement *) movieNode;
    NSArray *attributes = [movieElement attributes];

    for (NSXMLNode *attribute in attributes)
    {
        NSLog (@"%@ = %@", [attribute name], [attribute stringValue]);
    }
}

否则,你可以改用NSXMLParser。 这是一项由教区推动的活动,当其教职人员(除别的外)混杂时,会通知一名代表。 http://developer.apple.com/mac/library/documentation/coa/vis/NSXMLParserDelegate_ Protocol/Reference/Reference.html#/apple_ref/occ/intfm/NSXParserDelegate/parser:didStartElement:namespaceURI:standardName:attributes:"rel=“noreferer”> :parser:didartElement: 姓名:空间UR

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




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签