我使用下面的代碼:解析從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>**
有誰現在如何解決它?
我沒有看到任何地方你聲明XMLFILE。你需要指定它的類型 - 像'id xmlFile = ....' – Daniel
丹尼爾感謝你用PeterPeuGuo的答案來修正它! – Blazer