【发布时间】:2011-01-17 15:15:20
【问题描述】:
这与我之前的问题类似。我没有得到答案,也许通过改变问题我可能会得到答案。
这是我的解析代码:
-(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];
}
}
当我到达 parserDidEndDocument. theItems 类为空。
这里是 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
这里是 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
这是我的 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文件示例
<?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>
【问题讨论】:
-
如果可能的话,通过正确格式化代码并将其减少到相关部分,您将大大增加获得答案的机会。这很难阅读。
-
看起来每次启动元素时都会创建一个新的theItems,替换旧的但不保留旧的。所以基本上,你有一个巨大的内存泄漏,当你完成后,theItems 指向最后一个解析元素的数据。我猜这不是你想要的。
-
我试图格式化块中的代码,但对我来说,似乎相当困难。无论如何,即使我在 viewDidLoad 中创建了 theItems,我也会得到相同的结果。
-
尝试将项目作为属性访问 - 使用
self.theItems而不是theItems -
我试过了。
parseDidEndDocument为空
标签: iphone ios nsxmlparser