解析iOS中同名但不同字段的属性 [英] Parsing attributes with same name but in different fields in iOS

查看:25
本文介绍了解析iOS中同名但不同字段的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 php 如下:

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;
?>

这里我有两个字段,名称为 title,一个在频道中,另一个在项目属性中.我该如何单独解析它.我的解析代码如下:

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.

推荐答案

在 didStartElement 中,将当前标签推入堆栈.在 didEndElement 中,从堆栈中弹出它.然后你可以随时检查父元素的堆栈,并区分频道的标题元素和项目的标题元素.

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.

这篇关于解析iOS中同名但不同字段的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆