iphone xml解析器错误

时间:2010-03-11 18:08:30

标签: iphone xml xml-parsing

我正在尝试使用我的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>

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文件:

#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 方法后终止申请。

请你帮我解决这个问题。

感谢。

1 个答案:

答案 0 :(得分:0)

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

仅显示

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

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