解析从URL的XML目标C
我使用下面的代码:解析从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:
NSData * xmlFile = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: n];
您还需要声明类为:
@interface artikels : UIViewController<NSXMLParserDelegate> {
谢谢PeterPeiGuo我忘了,但在这一行:[rssParser setDelegate:self];我收到错误**发送'artikels * const__strong'tot参数的不兼容类型'id
谢谢!但在viewdidload我有[self parseXMLFileAtURL:@“http://****.com/get_recent_posts.xml”];但是当我启动应用程序时,我看到的唯一一件事是白页。但是在NSLOgs中,事物正在挑选4个'标题''(xml提要包含4个标题)。你可能知道问题是什么吗?谢谢! – Blazer
in .xib我只有一个视图对象,而且我只需要一个webview?或类似的东西? – Blazer
我没有看到任何地方你声明XMLFILE。你需要指定它的类型 - 像'id xmlFile = ....' – Daniel
丹尼尔感谢你用PeterPeuGuo的答案来修正它! – Blazer