【问题标题】:How to get a part of tag in XML (iOS)如何在 XML (iOS) 中获取标签的一部分
【发布时间】:2011-08-02 08:43:56
【问题描述】:

如何从 xml 文件的标签中获取一些数据?

我有一个这样的标签:

`<link href="http://127.0.0.1:8580/directory/playlists/" rel="alternate" type="application/atom+xml;type=feed" />`

我想保存http链接:http://127.0.0.1:8580/directory/playlists/

我可以使用 NSXMLParser 吗?

非常感谢!

【问题讨论】:

    标签: iphone xml ios xcode cocoa-touch


    【解决方案1】:

    您需要为符合NSXMLParserDelegate 协议的NSXMLParser 实现一个委托。在这个实现中至少实现这个方法:

    -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
                                           namespaceURI:(NSString *)namespaceURI 
                                          qualifiedName:(NSString *)qualifiedName 
                                             attributes:(NSDictionary *)attributeDict
    {
        if ([elementName isEqualToString:@"link"]) {
            NSString* href = [attributeDict objectForKey:@"href"];
            // Do you stuff with the href
        }
    } 
    

    【讨论】:

      【解决方案2】:

      “http://127.0.0.1:8580/directory/playlists/”之后的值称为属性。它是元素“链接”的属性。

      查看有关 XML 处理以及元素和属性的 Apple 文档。例如http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html

      【讨论】:

        猜你喜欢
        • 2012-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多