iPhone 的 xml 解析器错误

发布于 2024-08-25 12:48:52 字数 4203 浏览 2 评论 0原文

我正在尝试在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里泪两行 2024-09-01 12:48:52

查看源代码: http://api.stlouisfed.org/fred/ category_id=125&api_key=78da607fc8224651eca5653e65a4be5e

只显示

<categories>
<category id="125" name="Trade Balance" parent_id="13"/>
</categories>

可能不是原因,而是观察:-)

view-source: http://api.stlouisfed.org/fred/category?category_id=125&api_key=78da607fc8224651eca5653e65a4be5e

only shows

<categories>
<category id="125" name="Trade Balance" parent_id="13"/>
</categories>

Probably not the cause but an observation anyway :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文