【发布时间】:2012-08-05 13:57:31
【问题描述】:
我正在使用我正在编写的 XML 应用程序的教程,但我不知道如何将 NSURL 更改为 XML 文件在 iPhone 上的位置。
//XMLAppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSURL *url = [[NSURL alloc] initWithString:@"sites.google.com/site/iphonesdktutorials/xml/...";
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate [xmlParser setDelegate:parser];
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
【问题讨论】:
-
可能是
NSURL *url = [[NSURL alloc] initWithString:@"http://sites.google.com/site/iphonesdktutorials/xml/Books.xml"];? -
请发布您用于加载数据文件的代码。
-
我不希望它从 Web 服务中读取。我需要 iPhone 上的 XML 文件。该应用程序必须从那里读取它
-
//XMLAppDelegate.m - (void)applicationDidFinishLaunching:(UIApplication *)application { NSURL *url = [[NSURL alloc] initWithString:@"sites.google.com/site/iphonesdktutorials/xml/Books.xml"]; NSXMLParser *xmlParser = [[NSXMLParser alloc ] initWithContentsOfURL:url]; //初始化委托 XMLParser *parser = [[XMLParser alloc] initXMLParser]; //设置委托 [xmlParser setDelegate:parser]; BOOL success = [xmlParser parse]; if(success) NSLog(@ "No Errors"); else NSLog(@"Error Error Error!!!"); [window addSubview:[navigationController view]]; [window makeKeyAndVisible]; }
-
抱歉,无法将上面的代码格式化为好看的。该代码可在上面的教程链接中找到
标签: ios objective-c xml xml-parsing