Automatic parsing library in objective-c - [Automa

2019-07-02 02:59发布

Is there a library in Objective-C which I can use in IPhone, in which I can tell in advance the library that these tags it should expect in the xml file and then that library parse it automatically for me and give me an array of NSDictionary in return or something similar?

In simple words, I am looking for something which give me an array of "Object" after it has parse the xml document automatically and I can dictate attributes of that object which it should extract from the document.

I am looking for something similar to this in Objective-C http://code.google.com/p/google-gson/

4条回答
等我变得足够好
2楼-- · 2019-07-02 03:30

Though this question was posed more than one year ago, I add my own library licensed under the MIT license which does exactly what we are looking for:

http://ceasyxml.googlecode.com/

查看更多
干净又极端
3楼-- · 2019-07-02 03:37
劫难
4楼-- · 2019-07-02 03:48

I hope this answers your question, or give you some insight how you can aproach the problem. You want a simple way to convert your XML data to strong typed classes. Here is how I convert a XMLdata document to an Objective-C item called RSSDataItem. The XMLParser method, parseData takes your NSData from your Url (so its bytedata). I have two methods in my protocol (delegate).

my XMLParser interface (XMLParser.h)

@interface XMLParser : NSObject <NSXMLParserDelegate> 
{
    BOOL fStoreCharacters;
    NSXMLParser *parser;
    NSMutableString *currentData;
    RSSDataItem *currentItem;
}

- (void)parseData:(NSData*)data;
@property (assign) id <XMLParserDoneDelegate> delegate;

@end

Here is the protocol (delegate methods).

@protocol XMLParserDoneDelegate <NSObject>
- (void)itemParsed:(id)item; //one item parsed, tell delegate
- (void)parseDone; //we are done with all our data. Reload dataTable or what you want.
@end

MY Implementation file (*.m)

#import "XMLParser.h"

@implementation XMLParser
@synthesize delegate;


- (void)parseData:(NSData*)data
{
    [parser abortParsing];
    [parser release], parser = nil;

    parser = [[NSXMLParser alloc] initWithData:data];
    [parser setShouldProcessNamespaces:NO]; // We don't care about namespaces
    [parser setShouldReportNamespacePrefixes:NO]; //
    [parser setShouldResolveExternalEntities:NO]; // We just want data, no other stuff
    [parser setDelegate:self];

    [parser parse];
}

- (void)dealloc {

    delegate = nil;

    [currentItem release], currentItem = nil;
    [currentData release], currentData = nil;

    [parser abortParsing];
    [parser release], parser = nil;

    [super dealloc];
}

- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName 
    attributes:(NSDictionary *)attributeDict
{   
    if ([elementName isEqualToString:@"item"])
    {
        currentItem = [[RSSDataItem alloc] init];
    }
    else if ([elementName isEqualToString:@"title"]|| 
             [elementName isEqualToString:@"link"] || 
             [elementName isEqualToString:@"guid"] ||
             [elementName isEqualToString:@"description"] ||
             [elementName isEqualToString:@"pubDate"] )
    {
        [currentData setString:@""];
        fStoreCharacters = YES;
    }
}

- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"item"])
    {       
        if ([delegate respondsToSelector:@selector(itemParsed:)])
        {       
            [delegate itemParsed:currentItem];
        }

        [currentItem release], currentItem = nil;
    }
    else 
    {
        [currentItem setValue:currentData 
                       forKey:elementName];
    }

    fStoreCharacters = NO;  
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    if (parseError != nil ){
        NSLog(@"NSXMLParser: %@", [parseError localizedDescription]);
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (fStoreCharacters)
    {
        [currentData appendString:string];
    }   
}

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    currentData = [[NSMutableString alloc] init];
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    if ([delegate respondsToSelector:@selector(parseDone)])
        [delegate parseDone];
}


@end
查看更多
5楼-- · 2019-07-02 03:56

You should take a look at NSXMLParser and its delegate protocol, NSXMLParserDelegate. It is not as easy as what you described but not too hard to implement either.

查看更多
登录 后发表回答