求助:Three20 使用TTURLXMLResponse 获取XML问题

来源:互联网 发布:建筑软件大全 编辑:程序博客网 时间:2024/05/04 15:36

求助:Three20 使用TTURLXMLResponse 获取XML问题   

在写一个xml读取数据的程序。使用了 TTURLXMLResponse 这个 Three20 扩展。
根据网上找出的各种源码,已能成功读取远端的XML内容,但获取的内容 是一个 NSDictionary 对象,内容完全不象XML返回的内容
也许 NSDictionary 可以处理与读取,但我不会,向大家求助。


XML源码:


复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <rss version="2.0">
  3.   <magazines>
  4. <magazine>
  5.       <id>1</id>
  6.       <bid>0050</bid>
  7.       <title>Sourcing Magazine111111</title>
  8.       <catalogId>1</catalogId>
  9.       <img_57>http://192.168.1.2/iphone/images/icon_57.jpg</img_57>
  10.      <img_75>http://192.168.1.2/iphone/images/icon_320.jpg</img_75>
  11.       <link>http://192.168.1.2/iphone/zip/0001.zip</link>
  12.       <fileSize>20,000,000Byte</fileSize>
  13.       <description>This is Description in the Magazine</description>
  14.       <pubDate>2011-05-30 17:58:45</pubDate>
  15.      <preimg1>http://192.168.1.2/iphone/images/preimg1.jpg</preimg1>
  16.      <preimg2>http://192.168.1.2/iphone/images/preimg2/*.jpg</preimg2>
  17.      <preimg3>http://192.168.1.2/iphone/images/preimg3.jpg</preimg3>
  18.       </magazine>
  19. <magazine>
  20.       <id>2</id>
  21.       <bid>0050</bid>
  22.       <title>Sourcing Magazine2222</title>
  23.       <catalogId>1</catalogId>
  24.       <img_57>http://192.168.1.2/iphone/images/icon_57.jpg</img_57>
  25.      <img_75>http://192.168.1.2/iphone/images/icon_320.jpg</img_75>
  26.       <link>http://192.168.1.2/iphone/zip/0001.zip</link>
  27.       <fileSize>20,000,000Byte</fileSize>
  28.       <description>This is Description in the Magazine</description>
  29.       <pubDate>2011-05-30 17:58:45</pubDate>
  30.      <preimg1>http://192.168.1.2/iphone/images/preimg1.jpg</preimg1>
  31.      <preimg2>http://192.168.1.2/iphone/images/preimg2.jpg</preimg2>
  32.      <preimg3>http://192.168.1.2/iphone/images/preimg3.jpg</preimg3>
  33.       </magazine>
  34. <magazine>
  35.       <id>3</id>
  36.       <bid>0050</bid>
  37.       <title>Sourcing Magazine333</title>
  38.       <catalogId>1</catalogId>
  39.       <img_57>http://192.168.1.2/iphone/images/icon_57.jpg</img_57>
  40.      <img_75>http://192.168.1.2/iphone/images/icon_320.jpg</img_75>
  41.       <link>http://192.168.1.2/iphone/zip/0001.zip</link>
  42.       <fileSize>20,000,000Byte</fileSize>
  43.       <description>This is Description in the Magazine</description>
  44.       <pubDate>2011-05-30 17:58:45</pubDate>
  45.      <preimg1>http://192.168.1.2/iphone/images/preimg1.jpg</preimg1>
  46.      <preimg2>http://192.168.1.2/iphone/images/preimg2/*.jpg</preimg2>
  47.      <preimg3>http://192.168.1.2/iphone/images/preimg3.jpg</preimg3>
  48.       </magazine>
  49. </magazines>
  50. </rss>



很普通的XML

获取部分的源码
复制代码
  1. //读取,发送请求
  2. - (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more {
  3.     
  4.     if (!self.isLoading && TTIsStringWithAnyText(_searchQuery)) {
  5.         NSString* url = @"http://192.168.1.2/iphone/magazine.xml";
  6.         TTURLRequest* request = [TTURLRequest
  7.                                  requestWithURL: url
  8.                                  delegate: self];
  9.         request.cachePolicy = cachePolicy;
  10.         request.cacheExpirationAge = TT_CACHE_EXPIRATION_AGE_NEVER;
  11.         
  12.         TTURLXMLResponse* response = [[TTURLXMLResponse alloc] init];
  13.         response.isRssFeed = YES;
  14.         request.response = response;
  15.         TT_RELEASE_SAFELY(response);
  16.         
  17.         [request send];
  18.     }
  19. }
  20. //请求完成后,返回的内容处理
  21. - (void)requestDidFinishLoad:(TTURLRequest*)request {
  22. //获得接收的内容,处理
  23.     
  24.     TTURLXMLResponse* response = request.response;
  25.     TTDASSERT([response.rootObject isKindOfClass:[NSDictionary class]]);
  26.     
  27.     NSDictionary* feed = response.rootObject;
  28. //打印这个 NSDictionary 对象
  29.     NSLog(@"%@",feed);
  30. /*
  31. 这里是处理获得内容的地方,不会写。
  32. */
  33. //如果处理完成,请把获得的内容打成数组,然后显示到页面上
  34.    NSMutableArray* feeds = [[NSMutableArray alloc] init];
  35.     Magazine* f = [[Magazine alloc] init];
  36.     f.subject = @"Magazine";  //这里是最终读取的xml上的内容,为了显示,写死了
  37.     f.description = @"this is description";
  38.     f.img_57 = @"http://192.168.1.2/iphone/images/icon_57.jpg";    
  39.     [feeds addObject:f];
  40.     _feeds = feeds;
  41.     [super requestDidFinishLoad:request];
  42. }



打印出来 feed 的内容,应该是正确获取了,但如何处理,不清楚该怎么做
复制代码
  1. {
  2.     "___Entity_Name___" = rss;
  3.     "___Entity_Type___" = unknown;
  4.     "___Entity_Value___" = "\n  \n";
  5.     magazines =     {
  6.         "___Entity_Name___" = magazines;
  7.         "___Entity_Type___" = unknown;
  8.         "___Entity_Value___" = "\n\n\n\n\n";
  9.         magazine =         (
  10.                         {
  11.                 "___Entity_Name___" = magazine;
  12.                 "___Entity_Type___" = unknown;
  13.                 "___Entity_Value___" = "\n\t  \n\t  \n\t  \n\t  \n\t  \n     \n\t   \n\t  \n\t  \n\t  \n     \n     \n     \n\t  ";
  14.                 bid =                 {
  15.                     "___Entity_Name___" = bid;
  16.                     "___Entity_Type___" = unknown;
  17.                     "___Entity_Value___" = 0050;
  18.                 };
  19.                 catalogId =                 {
  20.                     "___Entity_Name___" = catalogId;
  21.                     "___Entity_Type___" = unknown;
  22.                     "___Entity_Value___" = 1;
  23.                 };
  24.                 description =                 {
  25.                     "___Entity_Name___" = description;
  26.                     "___Entity_Type___" = unknown;
  27.                     "___Entity_Value___" = "This is Description in the Magazine";
  28.                 };
  29.                 fileSize =                 {
  30.                     "___Entity_Name___" = fileSize;
  31.                     "___Entity_Type___" = unknown;
  32.                     "___Entity_Value___" = "20,000,000Byte";
  33.                 };
  34.                 id =                 {
  35.                     "___Entity_Name___" = id;
  36.                     "___Entity_Type___" = unknown;
  37.                     "___Entity_Value___" = 1;
  38.                 };
  39.                 "img_57" =                 {
  40.                     "___Entity_Name___" = "img_57";
  41.                     "___Entity_Type___" = unknown;
  42.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/icon_57.jpg";
  43.                 };
  44.                 "img_75" =                 {
  45.                     "___Entity_Name___" = "img_75";
  46.                     "___Entity_Type___" = unknown;
  47.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/icon_320.jpg";
  48.                 };
  49.                 link =                 {
  50.                     "___Entity_Name___" = link;
  51.                     "___Entity_Type___" = unknown;
  52.                     "___Entity_Value___" = "http://192.168.1.2/iphone/zip/0001.zip";
  53.                 };
  54.                 preimg1 =                 {
  55.                     "___Entity_Name___" = preimg1;
  56.                     "___Entity_Type___" = unknown;
  57.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/preimg1.jpg";
  58.                 };
  59.                 preimg2 =                 {
  60.                     "___Entity_Name___" = preimg2;
  61.                     "___Entity_Type___" = unknown;
  62.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/preimg2/*.jpg";
  63.                 };
  64.                 preimg3 =                 {
  65.                     "___Entity_Name___" = preimg3;
  66.                     "___Entity_Type___" = unknown;
  67.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/preimg3.jpg";
  68.                 };
  69.                 pubDate =                 {
  70.                     "___Entity_Name___" = pubDate;
  71.                     "___Entity_Type___" = unknown;
  72.                     "___Entity_Value___" = "2011-05-30 17:58:45";
  73.                 };
  74.                 title =                 {
  75.                     "___Entity_Name___" = title;
  76.                     "___Entity_Type___" = unknown;
  77.                     "___Entity_Value___" = "Sourcing Magazine111111";
  78.                 };
  79.             },
  80.                         {
  81.                 "___Entity_Name___" = magazine;
  82.                 "___Entity_Type___" = unknown;
  83.                 "___Entity_Value___" = "\n\t  \n\t  \n\t  \n\t  \n\t  \n     \n\t   \n\t  \n\t  \n\t  \n     \n     \n     \n\t  ";
  84.                 bid =                 {
  85.                     "___Entity_Name___" = bid;
  86.                     "___Entity_Type___" = unknown;
  87.                     "___Entity_Value___" = 0050;
  88.                 };
  89.                 catalogId =                 {
  90.                     "___Entity_Name___" = catalogId;
  91.                     "___Entity_Type___" = unknown;
  92.                     "___Entity_Value___" = 1;
  93.                 };
  94.                 description =                 {
  95.                     "___Entity_Name___" = description;
  96.                     "___Entity_Type___" = unknown;
  97.                     "___Entity_Value___" = "This is Description in the Magazine";
  98.                 };
  99.                 fileSize =                 {
  100.                     "___Entity_Name___" = fileSize;
  101.                     "___Entity_Type___" = unknown;
  102.                     "___Entity_Value___" = "20,000,000Byte";
  103.                 };
  104.                 id =                 {
  105.                     "___Entity_Name___" = id;
  106.                     "___Entity_Type___" = unknown;
  107.                     "___Entity_Value___" = 2;
  108.                 };
  109.                 "img_57" =                 {
  110.                     "___Entity_Name___" = "img_57";
  111.                     "___Entity_Type___" = unknown;
  112.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/icon_57.jpg";
  113.                 };
  114.                 "img_75" =                 {
  115.                     "___Entity_Name___" = "img_75";
  116.                     "___Entity_Type___" = unknown;
  117.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/icon_320.jpg";
  118.                 };
  119.                 link =                 {
  120.                     "___Entity_Name___" = link;
  121.                     "___Entity_Type___" = unknown;
  122.                     "___Entity_Value___" = "http://192.168.1.2/iphone/zip/0001.zip";
  123.                 };
  124.                 preimg1 =                 {
  125.                     "___Entity_Name___" = preimg1;
  126.                     "___Entity_Type___" = unknown;
  127.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/preimg1.jpg";
  128.                 };
  129.                 preimg2 =                 {
  130.                     "___Entity_Name___" = preimg2;
  131.                     "___Entity_Type___" = unknown;
  132.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/preimg2/*.jpg";
  133.                 };
  134.                 preimg3 =                 {
  135.                     "___Entity_Name___" = preimg3;
  136.                     "___Entity_Type___" = unknown;
  137.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/preimg3.jpg";
  138.                 };
  139.                 pubDate =                 {
  140.                     "___Entity_Name___" = pubDate;
  141.                     "___Entity_Type___" = unknown;
  142.                     "___Entity_Value___" = "2011-05-30 17:58:45";
  143.                 };
  144.                 title =                 {
  145.                     "___Entity_Name___" = title;
  146.                     "___Entity_Type___" = unknown;
  147.                     "___Entity_Value___" = "Sourcing Magazine2222";
  148.                 };
  149.             },
  150.                         {
  151.                 "___Entity_Name___" = magazine;
  152.                 "___Entity_Type___" = unknown;
  153.                 "___Entity_Value___" = "\n\t  \n\t  \n\t  \n\t  \n\t  \n     \n\t   \n\t  \n\t  \n\t  \n     \n     \n     \n\t  ";
  154.                 bid =                 {
  155.                     "___Entity_Name___" = bid;
  156.                     "___Entity_Type___" = unknown;
  157.                     "___Entity_Value___" = 0050;
  158.                 };
  159.                 catalogId =                 {
  160.                     "___Entity_Name___" = catalogId;
  161.                     "___Entity_Type___" = unknown;
  162.                     "___Entity_Value___" = 1;
  163.                 };
  164.                 description =                 {
  165.                     "___Entity_Name___" = description;
  166.                     "___Entity_Type___" = unknown;
  167.                     "___Entity_Value___" = "This is Description in the Magazine";
  168.                 };
  169.                 fileSize =                 {
  170.                     "___Entity_Name___" = fileSize;
  171.                     "___Entity_Type___" = unknown;
  172.                     "___Entity_Value___" = "20,000,000Byte";
  173.                 };
  174.                 id =                 {
  175.                     "___Entity_Name___" = id;
  176.                     "___Entity_Type___" = unknown;
  177.                     "___Entity_Value___" = 3;
  178.                 };
  179.                 "img_57" =                 {
  180.                     "___Entity_Name___" = "img_57";
  181.                     "___Entity_Type___" = unknown;
  182.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/icon_57.jpg";
  183.                 };
  184.                 "img_75" =                 {
  185.                     "___Entity_Name___" = "img_75";
  186.                     "___Entity_Type___" = unknown;
  187.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/icon_320.jpg";
  188.                 };
  189.                 link =                 {
  190.                     "___Entity_Name___" = link;
  191.                     "___Entity_Type___" = unknown;
  192.                     "___Entity_Value___" = "http://192.168.1.2/iphone/zip/0001.zip";
  193.                 };
  194.                 preimg1 =                 {
  195.                     "___Entity_Name___" = preimg1;
  196.                     "___Entity_Type___" = unknown;
  197.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/preimg1.jpg";
  198.                 };
  199.                 preimg2 =                 {
  200.                     "___Entity_Name___" = preimg2;
  201.                     "___Entity_Type___" = unknown;
  202.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/preimg2/*.jpg";
  203.                 };
  204.                 preimg3 =                 {
  205.                     "___Entity_Name___" = preimg3;
  206.                     "___Entity_Type___" = unknown;
  207.                     "___Entity_Value___" = "http://192.168.1.2/iphone/images/preimg3.jpg";
  208.                 };
  209.                 pubDate =                 {
  210.                     "___Entity_Name___" = pubDate;
  211.                     "___Entity_Type___" = unknown;
  212.                     "___Entity_Value___" = "2011-05-30 17:58:45";
  213.                 };
  214.                 title =                 {
  215.                     "___Entity_Name___" = title;
  216.                     "___Entity_Type___" = unknown;
  217.                     "___Entity_Value___" = "Sourcing Magazine3333";
  218.                 };
  219.             }
  220.         );
  221.     };
  222.     version = "2.0";
  223. }


自己研究。终于解决。源码奉上



//读取进程
- (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more {

    if (!self.isLoading && TTIsStringWithAnyText(_searchQuery)) {
              NSString* url = @"http://192.168.1.2/iphone/magazine.xml";
        TTURLRequest* request = [TTURLRequest
                                 requestWithURL: url
                                 delegate: self];
        request.cachePolicy = cachePolicy;
        request.cacheExpirationAge = TT_CACHE_EXPIRATION_AGE_NEVER;
        
        TTURLXMLResponse* response = [[TTURLXMLResponse alloc] init];
        response.isRssFeed = YES;
        request.response = response;
        
        TT_RELEASE_SAFELY(response);
        [request send];
    }    
}

- (void)requestDidFinishLoad:(TTURLRequest*)request {
    TTURLXMLResponse *response = request.response;
    TTDASSERT([response.rootObject isKindOfClass:[NSDictionary class]]);
    NSDictionary * rootObject = response.rootObject;
    TTDASSERT([[rootObject objectForKey:@"channel"] isKindOfClass:[NSDictionary class]]);
    NSDictionary *channel = [rootObject objectForKey:@"channel"];
    TTDASSERT([[channel objectForKey:@"item"] isKindOfClass:[NSDictionary class]]);
        
    NSMutableArray* feeds = [[NSMutableArray alloc] init];
    NSArray* entries =  [channel objectForKey:@"item"];
    
    for(int i=0;i<[entries count];i++){
    NSDictionary* entry = [entries objectAtIndex:i];
        Magazine* d = [[Magazine alloc] init];    
        NSDictionary* v2 = [entry objectForKey:@"title"];    
        d.subject = [v2 objectForKey:@"___Entity_Value___"];
        NSDictionary* v3 = [entry objectForKey:@"description"];    
        d.description = [v3 objectForKey:@"___Entity_Value___"];
        NSDictionary* v4 = [entry objectForKey:@"img_57"];    
        d.img_57 = [v4 objectForKey:@"___Entity_Value___"];
        [feeds addObject:d];
    }
     _feeds = feeds;
    
    [super requestDidFinishLoad:request];
}

返回的是一整套的类似JSON格式的东东,要反复打碎,解析,最后搞定。


转: http://www.cocoachina.com/bbs/read.php?tid-61730.html

原创粉丝点击