我不确定我做错了什么。我有一个URL,导致XML树看起来像:
<result>
...
<title>
...
</title>
<body>
...
</body>
...
</result>
我只需要解析文件并获取标题和正文。这是我的object.h:
#import <Foundation/Foundation.h>
@interface Object : NSObject
{
NSString *title;
NSString *description;
}
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *description;
@end
这是Object.m:
#import "Object.h"
@implementation Object
@synthesize title, description;
-(void) dealloc
{
[title release];
[description release];
[super dealloc];
}
@end
这是我的XMLParser.h:
#import <Foundation/Foundation.h>
#import "Object.h"
@interface XMLParser : NSObject <NSXMLParserDelegate>
{
NSMutableString *currentNodeContent;
NSMutableArray *arrayOfObjects;
NSXMLParser *parser;
Object *currentObject;
}
@property (readonly, retain) NSMutableArray *arrayOfObjects;
-(id) loadXMLbyURL:(NSString *)urlString;
@end
最后,XMLParser.m:
#import "XMLParser.h"
#import "Object.h"
@implementation XMLParser
@synthesize arrayOfObjects;
-(id) loadXMLbyURL:(NSString *)urlString
{
arrayOfObjects = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[[NSData alloc] initWithContentsOfURL:url] autorelease];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;
}
-(void) dealloc
{
[parser release];
[super dealloc];
}
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"result"])
{
currentObject = [Object alloc];
}
}
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"title"])
{
currentObject.title = currentNodeContent;
}
if([elementName isEqualToString:@"body"])
{
currentObject.description = currentNodeContent;
}
if([elementName isEqualToString:@"result"])
{
[arrayOfObjects addObject:currentObject];
[currentObject release];
currentObject = nil;
[currentNodeContent release];
currentNodeContent = nil;
}
}
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
@end
在我的主代码中,我像这样调用XML解析器:
xmlParser = [[XMLParser alloc] loadXMLbyURL:@"www.websiteWithXML.com"];
然而,我的arrayOfObjects中没有任何内容(我知道这一点,因为当我告诉tableview有与我的数组一样多的行时,没有行)。
请帮忙!!!提前谢谢!
答案 0 :(得分:0)
不确定这是否有帮助...
尝试更改:
arrayOfObjects = [[NSMutableArray alloc] init];
要:
arrayOfObjects = [[NSMutableArray alloc] initWithCapacity:0];
除此之外,您的代码似乎没问题。也只是因为你的表没有加载任何东西并不意味着你的数组中没有对象。完成解析后以及尝试加载tableview之前,请使用断点查看数据。
答案 1 :(得分:0)
尝试在每个元素启动时重置currentNodeContent。例如:
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"result"])
{
currentObject = [Object alloc];
}
else if ([elementName isEqualToString:@"title"] || [elementName isEqualToString:@"body"])
{
[currentNodeContent setString:@""];
}
}
然后,当你收到字符时,请添加:
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[currentNodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
通过执行此操作,您可以处理foundCharacters多次获取以捕获给定元素中的所有字符的情况。