This is similar to my previous question. I didn’t get an answer, maybe by changing the question I might get an answer.
Here is my parsing code:
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName
namespaceURI:(NSString *) namespaceURI
qualifiedName:(NSString *) qName
attributes:(NSDictionary *) attributeDict
{
if ([elementName isEqualToString:kimgurl]
|| [elementName isEqualToString:kone_x]
|| [elementName isEqualToString:kone_y]
|| [elementName isEqualToString:kone_radius]
|| [elementName isEqualToString:ktwo_x]
|| [elementName isEqualToString:ktwo_y]
|| [elementName isEqualToString:ktwo_radius])
{
elementFound = YES;
theItems = [[Items alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:kimgurl])
{
theItems.imageURL = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:kone_x])
{
theItems.iOne_X = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:kone_y])
{
theItems.iOne_Y = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:kone_radius])
{
theItems.iOne_Radius = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:ktwo_x])
{
theItems.iTwo_X = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:ktwo_y])
{
theItems.iTwo_Y = self.currentValue;
[self.currentValue setString:@""];
}
else if([elementName isEqualToString:ktwo_radius])
{
theItems.iTwo_Radius = self.currentValue;
[self.currentValue setString:@""];
}
}
-(void) parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"enddocument: %@", theItems.imageURL);
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFound == YES) {
if(!currentValue)
{
currentValue = [NSMutableString string];
}
[currentValue appendString: string];
}
}
When I get to parserDidEndDocument. The theItems class is empty.
Here is Items.h
#import <Foundation/Foundation.h>
@interface Items : NSObject {
@private
//parsed data
NSString *imageURL;
NSString *iOne_X;
NSString *iOne_Y;
NSString *iOne_Radius;
NSString *iTwo_X;
NSString *iTwo_Y;
NSString *iTwo_Radius;
}
@property (nonatomic, retain) NSString *imageURL;
@property (nonatomic, retain) NSString *iOne_X;
@property (nonatomic, retain) NSString *iOne_Y;
@property (nonatomic, retain) NSString *iOne_Radius;
@property (nonatomic, retain) NSString *iTwo_X;
@property (nonatomic, retain) NSString *iTwo_Y;
@property (nonatomic, retain) NSString *iTwo_Radius;
@end
here is Items.m
#import "Items.h"
@implementation Items
@synthesize imageURL;
@synthesize iOne_X;
@synthesize iOne_Y;
@synthesize iOne_Radius;
@synthesize iTwo_X;
@synthesize iTwo_Y;
@synthesize iTwo_Radius;
-(void)dealloc
{
[imageURL release];
[iOne_X release];
[iOne_Y release];
[iOne_Radius release];
[iTwo_X release];
[iTwo_Y release];
[iTwo_Radius release];
[super dealloc];
}
@end
here is my RootViewController.h
#import <UIKit/UIKit.h>
@class Items;
@interface RootViewController : UIViewController <NSXMLParserDelegate> {
NSMutableData *downloadData;
NSURLConnection *connection;
BOOL elementFound;
NSMutableString *currentValue;
NSMutableDictionary *pictures;
//---xml parsing---
NSXMLParser *xmlParser;
Items *theItems;
NSMutableArray *aItems;
}
@property (nonatomic, retain) Items *theItems;
@property (nonatomic, retain) NSMutableArray *aItems;
@property (nonatomic, retain) NSMutableString *currentValue;
@property (nonatomic, retain) NSMutableData *downloadData;
@property (nonatomic, retain) NSURLConnection *connection;
@end
xml file example
<?xml version="1.0" encoding="utf-8"?>
<data>
<test>
<url>url</url>
<one_x>83</one_x>
<one_y>187</one_y>
<one_radius>80</one_radius>
<two_x>183</two_x>
<two_y>193</two_y>
<two_radius>76</two_radius>
</test>
</data>
It looks like there are a couple of potential problems. In your didStartElement method you are alloc/init’ing a new Items object for every element and overwriting your previous one. Perhaps you can move the Items init into your –parserDidStartDocument: method. When you init, it should also look more like this:
Items *items = [[Items alloc] init];
self.theItems = items;
[items release];
Then you’ll have the correct retain count when you are done.
I’d also recommend changing your NSString @property declarations to be copy instead of retain. The code:
… isn’t doing what you think. theItems.imageURL is going to be pointing at your NSMutableString and then you clear the mutable string right after which means imageURL is pointing at an empty mutable string. Then after all of the other iterations, all of them are pointing at the same NSMutableString which is empty. If you change the @property declarations to copy, then it’ll set imageURL to an immutable NSString copy of the contents of self.currentValue.