2015-02-11 157 views
-2

我有一個xml解析:XML解析器目標-C

<media:content url='http://www.youtube.com/v/x5cBBXXFAPQ?version=3&amp;f=playlists&amp;app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='391' yt:format='5'/><media:content url='rtsp://r5---sn-4g57kuee.c.youtube.com/CiULENy73wIaHAn0AMV1BQGXxxMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='391' yt:format='1'/><media:content url='rtsp://r5---sn-4g57kuee.c.youtube.com/CiULENy73wIaHAn0AMV1BQGXxxMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='391' yt:format='6'/><media:description type='plain'>Bohemian Rhapsody 

我怎樣才能從內容的第一網址是什麼?

+1

標識說:你有什麼嘗試,這是太寬泛了,所以SO不是爲我寫'我的代碼'類頁面...但我不.. ..而是我給你的代碼 – 2015-02-11 10:43:49

+0

可能的重複[從Objective-C導航XML(http://stackoverflow.com/questions/762567/navigating-xml-from-objective-c) – Larme 2015-02-11 12:39:14

回答

0

運行的樣本: 編輯的基礎上評論...

#import <Foundation/Foundation.h> 

@interface MyClass : NSObject<NSXMLParserDelegate> 
@end 
@implementation MyClass 

- (instancetype)init { 
    self = [super init]; 
    id xml = @"<media:content url='http://www.youtube.com/v/x5cBBXXFAPQ?version=3&amp;f=playlists&amp;app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='391' yt:format='5'/><media:content url='rtsp://r5---sn-4g57kuee.c.youtube.com/CiULENy73wIaHAn0AMV1BQGXxxMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='391' yt:format='1'/><media:content url='rtsp://r5---sn-4g57kuee.c.youtube.com/CiULENy73wIaHAn0AMV1BQGXxxMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='391' yt:format='6'/><media:description type='plain'>Bohemian Rhapsody"; 
    id d = [xml dataUsingEncoding:NSUTF8StringEncoding]; 

    NSXMLParser *p = [[NSXMLParser alloc] initWithData:d]; 
    p.delegate = self; 
    [p parse]; 

    return self; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 
    if([elementName isEqualToString:@"media:content"]) { 
     static BOOL first = YES; 
     if(first) { 
      NSLog(@"%@", attributeDict[@"url"]); 
      first = NO; 
     } 
    } 
} 

@end 

int main(int argc, const char * argv[]) { 
    @autoreleasepool { 
     id c = [[MyClass alloc] init]; 
    } 
    return 0; 
} 
+0

這是適用於我所有的網址,但我只想從第一個網址內容:( – 2015-02-11 11:09:40

+0

怎麼樣只保存第一個呢? ;) – 2015-02-11 12:35:39

0

首先,你需要參考蘋果機制的文檔

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/index.html

- (void)viewDidLoad 

    { 
     NSURL *url = [[NSURL alloc]initWithString:@"yourURL"]; 
     NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:url]; 
     [parser setDelegate:self]; 
     BOOL result = [parser parse]; 
    } 

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 
    { 
     NSLog(@\"Did start element\"); 
    if ([elementName isEqualToString:@"media:content"]) 
    { 
     NSLog(@"found URL",[attributeDict objectForKey:@"url"]);//here u will get the url u want 
     return; 
    } 
    } 

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
    { 
     NSLog(@"Did end element"); 
     if ([elementName isEqualToString:@"media"]) 
      { 
       NSLog(@"rootelement end"); 
      } 

    } 
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
    { 

      NSLog(@"Value %@",string); 

    } 

它只是概述有關的委託方法XML不是解決方法..請參閱文檔..