我是 Objective C 的初学者,我想从 URL 解析一个 XML 文件,我找到了一些关于 NSXMLPARSER 的示例代码,我编写了这段代码,但它不起作用。
请帮我。
我的 xml 文件是:
<list>
<first>apple</first>
</list>
...我的 ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <NSXMLParserDelegate> {
}
@property (weak, nonatomic) IBOutlet UITextView *myTextField;
@end
...我的 ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController @synthesize myTextField;
- (void)parseXMLFileAtURL:(NSString *)URL {
NSURL * xmlURL = [NSURL URLWithString:URL];
NSXMLParser * rssparser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssparser setDelegate:self];
[rssparser parse];
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary*)attributeDict {
if ([elementName isEqualToString:@"list"]) {
// clear out our story item caches...
myTextField.text = [attributeDict objectForKey:@"first"];
}
}
- (void)viewDidLoad {
NSString * path = @"http://example.com";
[self parseXMLFileAtURL:path];
[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload {
[self setMyTextField:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end