iPhone 的 xml 解析器错误
我正在尝试在我的 iPhone 应用程序中使用 Web 服务来实现和解析 xml 文档:
这是我的请求(HTTP GET):
http://api.stlouisfed.org/fred/category?category_id=125&api_key=78da607fc8224651eca5653e65a4be5e
响应将是:
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<category id="125" name="Trade Balance" parent_id="13"/>
</categories>
我的 testxml .h:
#import <UIKit/UIKit.h>
@interface testxmlViewController : UIViewController {
IBOutlet UILabel *lbl1;
IBOutlet UILabel *lbl2;
IBOutlet UIButton *submit;
NSMutableData *categoryData;
NSURLConnection *URLConnection;
NSMutableString *currnetCategory;
BOOL *hello;
}
@property (nonatomic, retain) NSMutableData *categoryData;
@property (nonatomic, retain) NSURLConnection *URLConnection;
@property (nonatomic, retain) NSMutableString *currnetCategory;
- (IBAction) submitClick :(id) sender;
@end
我的 testxml.m 文件:
#import "testxmlViewController.h"
@implementation testxmlViewController
@synthesize categoryData, URLConnection;
@synthesize currnetCategory;
- (IBAction) submitClick :(id) sender
{
lbl1.text = @"Submit button was clicked.";
static NSString *URLString = @"http://api.stlouisfed.org/fred/category?category_id=125&api_key=78da607fc8224651eca5653e65a4be5e";
NSURLRequest *URLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString]];
URLConnection = [[[NSURLConnection alloc] initWithRequest:URLRequest delegate:self] autorelease];
NSAssert(self.URLConnection != nil, @"Failure to create URL connection.");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.categoryData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[categoryData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"ERROR with theConenction");
[connection release];
[categoryData release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *theXML = [[NSString alloc] initWithBytes: [categoryData mutableBytes] length:[categoryData length] encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXML release];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:categoryData];
if( parser )
{
[parser release];
}
parser = [[NSXMLParser alloc] initWithData: categoryData];
[parser setDelegate: self];
[parser setShouldResolveExternalEntities: YES];
[parser parse];
[connection release];
[categoryData release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(elementName);
if ([elementName isEqualToString:@"category"])
{
lbl1.text = [attributeDict objectForKey:@"name"];
NSLog([attributeDict objectForKey:@"name"]);
//currnetCategory = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (currnetCategory)
{
[currnetCategory appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(elementName);
}
我在 **GDB 中正确打印了所有值:**
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<category id="125" name="Trade Balance" parent_id="13"/>
</categories>
2010-03-11 10:05:57.817 testxml[16203:207] categories
2010-03-11 10:05:57.819 testxml[16203:207] category
2010-03-11 10:05:57.822 testxml[16203:207] Trade Balance
2010-03-11 10:05:57.823 testxml[16203:207] category
2010-03-11 10:05:57.823 testxml[16203:207] categories
但它在之后终止了应用程序执行didEndElement方法。
请你帮我解决这个问题好吗?
谢谢。
I'm trying to implement and parsing xml document using web service in my iphone application:
Here is my Request (HTTP GET):
http://api.stlouisfed.org/fred/category?category_id=125&api_key=78da607fc8224651eca5653e65a4be5e
Response will be:
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<category id="125" name="Trade Balance" parent_id="13"/>
</categories>
my testxml.h :
#import <UIKit/UIKit.h>
@interface testxmlViewController : UIViewController {
IBOutlet UILabel *lbl1;
IBOutlet UILabel *lbl2;
IBOutlet UIButton *submit;
NSMutableData *categoryData;
NSURLConnection *URLConnection;
NSMutableString *currnetCategory;
BOOL *hello;
}
@property (nonatomic, retain) NSMutableData *categoryData;
@property (nonatomic, retain) NSURLConnection *URLConnection;
@property (nonatomic, retain) NSMutableString *currnetCategory;
- (IBAction) submitClick :(id) sender;
@end
my testxml.m file:
#import "testxmlViewController.h"
@implementation testxmlViewController
@synthesize categoryData, URLConnection;
@synthesize currnetCategory;
- (IBAction) submitClick :(id) sender
{
lbl1.text = @"Submit button was clicked.";
static NSString *URLString = @"http://api.stlouisfed.org/fred/category?category_id=125&api_key=78da607fc8224651eca5653e65a4be5e";
NSURLRequest *URLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString]];
URLConnection = [[[NSURLConnection alloc] initWithRequest:URLRequest delegate:self] autorelease];
NSAssert(self.URLConnection != nil, @"Failure to create URL connection.");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.categoryData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[categoryData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"ERROR with theConenction");
[connection release];
[categoryData release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *theXML = [[NSString alloc] initWithBytes: [categoryData mutableBytes] length:[categoryData length] encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXML release];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:categoryData];
if( parser )
{
[parser release];
}
parser = [[NSXMLParser alloc] initWithData: categoryData];
[parser setDelegate: self];
[parser setShouldResolveExternalEntities: YES];
[parser parse];
[connection release];
[categoryData release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(elementName);
if ([elementName isEqualToString:@"category"])
{
lbl1.text = [attributeDict objectForKey:@"name"];
NSLog([attributeDict objectForKey:@"name"]);
//currnetCategory = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (currnetCategory)
{
[currnetCategory appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(elementName);
}
I'm getting all the values printed correctly in my **GDB :**
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<category id="125" name="Trade Balance" parent_id="13"/>
</categories>
2010-03-11 10:05:57.817 testxml[16203:207] categories
2010-03-11 10:05:57.819 testxml[16203:207] category
2010-03-11 10:05:57.822 testxml[16203:207] Trade Balance
2010-03-11 10:05:57.823 testxml[16203:207] category
2010-03-11 10:05:57.823 testxml[16203:207] categories
But it was terminating application after execution of didEndElement method.
Will you please help me to solve this problem.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看源代码: http://api.stlouisfed.org/fred/ category_id=125&api_key=78da607fc8224651eca5653e65a4be5e
只显示
可能不是原因,而是观察:-)
view-source: http://api.stlouisfed.org/fred/category?category_id=125&api_key=78da607fc8224651eca5653e65a4be5e
only shows
Probably not the cause but an observation anyway :-)