Automated parsing library in objective-c - [Automated conversion of XML / JSON to Object Conversion]

Is there a library in Objective-C that I can use on IPhone in which I can tell the library in advance that these labels should be expected in an XML file, and then this library will automatically parse it for me and give me an NSDictionary array in response, or what something like that?

In simple words, I'm looking for something that gives me an “Object” array after it parses the XML document automatically, and I can dictate the attributes of this object that it should extract from the document.

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

+3
source share
4
+1

NSXMLParser , NSXMLParserDelegate. , , .

0

, , . XML- . XMLdata Objective-C, RSSDataItem. XMLParser, parseData NSData URL ( bytedata). ().

XMLParser (XMLParser.h)

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

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

@end

( ).

@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

(*.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
0

Source: https://habr.com/ru/post/1794866/


All Articles