开发者

NSXMLParser Issue

开发者 https://www.devze.com 2023-02-04 11:38 出处:网络
I have an xml file I am parsing. NSURL *url = [NSURL URLWithString:@\"url.xml\"]; NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeou

I have an xml file I am parsing.

NSURL *url = [NSURL URLWithString:@"url.xml"];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
self.downloadData = [[NSMutableData alloc] initWithLeng开发者_开发知识库th:0];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

I have all the connections working and I thought I had the parsing working but I am having issues and do not know what I am doing wrong.

my didStartElement:

-(void) parser:(NSXMLParser *) parser  didStartElement:(NSString *) elementName  namespaceURI:(NSString *) namespaceURI  qualifiedName:(NSString *) qName   attributes:(NSDictionary *) attributeDict {
    if ([elementName isEqualToString:kimgurl])
    {
        elementFound = YES;
    }
}

foundCharacter:

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
if (elementFound == YES) {
         if(!currentValue)
         {
             currentValue = [[NSMutableString alloc] init];
         }

        [currentValue appendString: string];
    }
}

then didEndElement:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {  
    if (elementFound) {
        if ([elementName isEqualToString:kimgurl]) {
            NSLog(@"imgurl: %@", currentValue);
            imageItems.imageURL = currentValue;
            NSLog(@"elname: %@", elementName);
            NSLog(@"img: %@", kimgurl);
            [currentValue setString:@""];
            NSLog(@"imageitem: %@", imageItems.imageURL);
        }
    }
}

I have the NSLogs there because imageItems.imageURL is null. This is a class file that is like this.

ImageItems.h

@interface ImageItems : NSObject {
    //parsed data
    NSString *imageURL;
}

@property (nonatomic, retain) NSString *imageURL;

@end

ImageItems.m

#import "ImageItems.h"

@implementation ImageItems    
@synthesize imageURL;

-(void)dealloc
{
    [imageURL release];
    [super dealloc];
}

@end

As you can tell by my code I am new to objective-c.

currentValue has the value Im looking for. Why is imageItems null? What am I missing?


I don't see the assignment of "imageItems" anywhere in your code. You probably need to assign it to an instance of ImageItems at some point. Perhaps with self.imageItems = [[[ImageItems alloc] init] autorelease] or imageItems = [[ImageItems alloc] init];?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号