2011-12-11 122 views
0

我使用下面的代碼:解析從URL的XML目標C

- (void)parserDidStartDocument:(NSXMLParser *)parser{ 
NSLog(@"File found and parsing started"); 

} 


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


NSString *agentString = @"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1"; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: 
[NSURL URLWithString:URL]]; 
[request setValue:agentString forHTTPHeaderField:@"User-Agent"]; 

xmlFile = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ]; 


articles = [[NSMutableArray alloc] init]; 
errorParsing= NO; 

rssParser = [[NSXMLParser alloc] initWithData:xmlFile]; 

[rssParser setDelegate:self]; 

// You may need to turn some of these on depending on the type of XML file you are parsing 
[rssParser setShouldProcessNamespaces:NO]; 
[rssParser setShouldReportNamespacePrefixes:NO]; 
[rssParser setShouldResolveExternalEntities:NO]; 

[rssParser parse]; 

} 


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

NSString *errorString = [NSString stringWithFormat:@"Error code %i", [parseError code]]; 
NSLog(@"Error parsing XML: %@", errorString); 


errorParsing=YES; 
} 


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
currentElement = [elementName copy]; 
ElementValue = [[NSMutableString alloc] init]; 
if ([elementName isEqualToString:@"intro"]) { 
item = [[NSMutableDictionary alloc] init]; 

} 

} 


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
[ElementValue appendString:string]; 
} 

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

if (errorParsing == NO) 
{ 
NSLog(@"XML processing done!"); 
} else { 
NSLog(@"Error occurred during XML processing"); 
} 

} 

但是就行了:

xmlFile = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ];

和:

rssParser = [[NSXMLParser alloc] initWithData:xmlFile]; 

[rssParser setDelegate:self]; 

我得到了2個錯誤:

Use of undeclared identifier 'xmlFile'

我.h文件中:

#import <UIKit/UIKit.h> 

@class ontfsViewController; 


@interface artikels : UIViewController { 

    ontfsViewController *detailViewController; 

    IBOutlet UIWebView *errorView; 

    //xml 
    NSXMLParser *rssParser; 
    NSMutableArray *articles; 
    NSMutableDictionary *item; 
    NSString *currentElement; 
    NSMutableString *ElementValue; 
    BOOL errorParsing; 
} 

@property (nonatomic, retain) IBOutlet ontfsViewController *detailViewController; 

@end 

而且我viewDidLoad中:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    [self parseXMLFileAtURL:@"http://****.com/get_recent_posts.xml"]; 


} 

和:

[rssParser setDelegate:self]; 


**Sending 'artikels *const__strong' tot parameter of incompatible type 'id<NSXMLPARSERDELEGATE>** 

有誰現在如何解決它?

+0

我沒有看到任何地方你聲明XMLFILE。你需要指定它的類型 - 像'id xmlFile = ....' – Daniel

+0

丹尼爾感謝你用PeterPeuGuo的答案來修正它! – Blazer

回答

0

需要聲明XMLFILE:

NSData * xmlFile = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: n]; 

您還需要聲明類爲:

@interface artikels : UIViewController<NSXMLParserDelegate> { 
+0

謝謝PeterPeiGuo我忘了,但在這一行:[rssParser setDelegate:self];我收到錯誤**發送'artikels * const__strong'tot參數的不兼容類型'id ** – Blazer

+0

謝謝!但在viewdidload我有[self parseXMLFileAtURL:@「http://****.com/get_recent_posts.xml」];但是當我啓動應用程序時,我看到的唯一一件事是白頁。但是在NSLOgs中,事物正在挑選4個'標題''(xml提要包含4個標題)。你可能知道問題是什麼嗎?謝謝! – Blazer

+0

in .xib我只有一個視圖對象,而且我只需要一個webview?或類似的東西? – Blazer