不同字段我有一個PHP如下:解析具有相同名稱的屬性,但在在IOS
<?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;
?>
這裏,我已經具有名稱標題兩個字段,一個在通道和其他在項目屬性。我如何分別解析它。我的解析代碼如下:
-(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");
}
現在,問題是,當我顯示在表視圖進料,兩種標題字段被越來越所附但我需要只顯示信道屬性的標題字段中。
請幫助我,讓我知道我做錯了什麼。
在這方面,正確的代碼將是巨大的幫助。
爲什麼不嘗試使用[TouchXML](https://github.com/TouchCode/TouchXML)? – Ilanchezhian 2012-02-02 06:59:52