本文介绍了XMLParser建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我做错了什么。我有一个URL,通向一个XML树,如下所示:

I'm not sure what I am doing wrong. I have a URL leading to an XML tree that looks like:

<result>
  ...
  <title>
  ...
  </title>
  <body>
  ...
  </body>
  ...
</result>

我只需要解析文件并获取标题和正文。这是我的object.h:

I just need to parse the file and get the title and body. Here is my 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:

And here is Object.m:

#import "Object.h"

@implementation Object

@synthesize title, description;

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

@end

这是我的XMLParser.h :

Here is my 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:

And finally, 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解析器:

In my main code I make a call to XML parser like this:

xmlParser = [[XMLParser alloc] loadXMLbyURL:@"www.websiteWithXML.com"];

然而,我的arrayOfObjects中没有任何内容(我知道这是因为当我告诉tableview有as时很多行作为我的数组,没有行)。

However nothing is being placed into my arrayOfObjects (I know this because when I tell a tableview to have as many rows as my array, there are no rows).

请帮忙!提前谢谢!

推荐答案

尝试在每个元素启动时重置currentNodeContent。例如:

Try resetting currentNodeContent inside each of your element starts. For example:

-(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:@""];
    }
}

然后,当你收到字符时,请追加:

Then, when you receive characters do an append:

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [currentNodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}

通过执行此操作,您可以处理foundCharacters多次捕获的情况给定元素中的所有字符。

By doing this, you handle the situation where a foundCharacters gets multiple times to capture all of the characters within a given element.

这篇关于XMLParser建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 14:41