doucezhu3570 2012-02-02 06:46
浏览 44
已采纳

解析具有相同名称但在iOS中的不同字段中的属性

I have a php as follows:

<?php
    header("Content-Type: application/rss+xml; charset=ISO-8859-1");
    $ip=$_GET['ip'];
    $type=$_GET['type'];
    $rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $rssfeed .= '<rss version="2.0">';
    $connection = mysql_connect('localhost','root')
    or die('Could not connect to database');
    mysql_select_db('Android')
    or die ('Could not select database');
    $query = "SELECT * FROM User_Upload_Table WHERE Status='Approved' and Content_Type='$type' ORDER BY Approved_Time desc";
    $result = mysql_query($query) or die ("Could not execute query");
    while($row = mysql_fetch_array($result)) {
            extract($row);
            $rssfeed .= "<channel>";
            $rssfeed .= "<title>" .$row[Content_Name] ."</title>";
            $rssfeed .= '<link>http://'.$ip .$row[Content_Path] .$row[Status] . '/' .$row[Content_Name] . '</link>';
            if($type == "Video"){
          $rssfeed .= '<description>' .$row[Duration]. '</description>';
            }
            $rssfeed .= '<category>' .$row[Description]. '</category>';
            $rssfeed .= '<rating>' .$row[Rating]. '</rating>';
            $rssfeed .= '<generator>' .$row[Vote_Count]. '</generator>';
            $rssfeed .= '<language></language>';
            if($type == "Video"){
              $name = $row[Content_Name];
              $subName = substr($name, 0, strpos($name, '.'));
              $rssfeed .= '<image>http://'.$ip.'/Android/'.$type.'/'.$subName.'.jpg</image>';
            } else {
              $rssfeed .= '<image>http://'.$ip.'/Android/'.$type.'/Approved.jpg</image>';
            }
            $rssfeed .= '<copyright>Copyright 2011</copyright>';
            $rssfeed .= '<item>';
            $rssfeed .= '<title>Pre-Roll</title>';
            $rssfeed .= '<link>http://'.$ip.'/Android/Video/Approved/MERCEDES_BENZ.3gp</link>';
            $rssfeed .= '<description>Post-Roll</description>';
            $rssfeed .= '<source>http://'.$ip.'/Android/Video/Approved/PG_Dawn_PGDN4582000_30.mp4</source>';
            $rssfeed .= '</item>';
            $rssfeed .= '</channel>';
    }
    $rssfeed .= '</rss>';
    echo $rssfeed;
?>

Here i have two fields with the name title, one in channel and the other in item attribute. How can I parse it separately. My parsing code is as follows:

  -(void)parseXMLFileAtURL:(NSString *)URL{

  NSURL *xmlURL = [NSURL URLWithString:URL];
  rssParser = [[NSXMLParser alloc]initWithContentsOfURL:xmlURL];
  [rssParser setDelegate:self];
  [rssParser setShouldProcessNamespaces:NO];
  [rssParser setShouldReportNamespacePrefixes:NO];
  [rssParser setShouldResolveExternalEntities:NO];
  [rssParser parse];
  NSLog(@"Parsed");
}

-(void)parserDidStartDocument:(NSXMLParser *)parser{

    NSLog(@"Found file and started parsing");
}


-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{

NSString *errorString = [NSString stringWithFormat:@"Unable to download feed from website (Error Code %i)", [parseError code]];
NSLog(@"Error parsing xml: %@", errorString);
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}


-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

  insideItem = FALSE;
  insideChannel = FALSE;

  rssElement      = [[NSMutableDictionary alloc]init];

  currentElement = [elementName copy];
  if ([elementName isEqualToString:@"channel"]) {

    title           = [[NSMutableString alloc]init];
    link            = [[NSMutableString alloc]init];
    description     = [[NSMutableString alloc]init];
    copyright       = [[NSMutableString alloc]init];
    image           = [[NSMutableString alloc]init];
  }
}


-(void)parser: (NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

  if ([elementName isEqualToString:@"channel"]) {

    [rssElement setObject:title                 forKey:@"title"];
    [rssElement setObject:link                  forKey:@"link"];
    [rssElement setObject:description           forKey:@"description"];
    [rssElement setObject:copyright             forKey:@"copyright"];
    [rssElement setObject:image                 forKey:@"image"];

    [item addObject:[rssElement copy]];
    NSLog(@"adding stories %@", title);

  }
}


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

  if ([currentElement isEqualToString:@"title"]) {
    [title appendString:string];
  }else if ([currentElement isEqualToString:@"link"]) {
    [link appendString:string];
  }else if ([currentElement isEqualToString:@"description"]) {
    [description appendString:string];
  }else if ([currentElement isEqualToString:@"copyright"]) {
    [copyright appendString:string];
  }else if ([currentElement isEqualToString:@"image"]) {
     [image appendString:string];
  }
}

-(void)parserDidEndDocument:(NSXMLParser *)parser{

    NSLog(@"all done");
    NSLog(@"item array has %d items", [item count]);
    [tableView reloadData];
    NSLog(@"Finished Parsing");
}

Now, the problem is when I am displaying the feed in a table view, the two title fields are getting appended but I need to display only the title field of channel attribute.

Please help me and let me know what is that I am doing wrong.

The proper code will be of immense help in this regard.

  • 写回答

2条回答 默认 最新

  • douchuntang2827 2012-02-02 15:28
    关注

    In didStartElement, push the current tag to a stack. In didEndElement, pop it from the stack. Then you can always check the stack for the parent element and distinguish between the channel's title element and the item's title element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置