Elysia Tools

Explore ferramentas, exemplos, explicações visuais e guias temáticos em um espaço claro.

Categorias populares

Temas

Idioma
ZHDERUPTENFRES

Elysia Tools

Navegação

Objective-C

Serialização macOS Objective-C - Exemplos

Exemplos de serialização macOS Objective-C incluindo serialização JSON, desserialização JSON e análise XML

Exemplos

Entradas desta coleção

CódigoobjectivecIntermediário

Serialização JSON

Converter objetos Objective-C em strings JSON usando NSJSONSerialization

Dificuldade

5/10

Tempo estimado

25 min

Etiquetas

objectivec, macos, serialization, json

Pré-requisitos

Objective-C basics, Foundation framework

objectivec
// macOS Objective-C JSON Serialization Examples
// Using NSJSONSerialization for JSON encoding

#import <Foundation/Foundation.h>

// MARK: - 1. Serialize Dictionary to JSON

@interface JSONSerializer : NSObject

+ (NSData *)serializeDictionaryToJSON:(NSDictionary *)dictionary;
+ (NSString *)serializeDictionaryToJSONString:(NSDictionary *)dictionary;
+ (BOOL)writeDictionaryToJSONFile:(NSDictionary *)dictionary path:(NSString *)path;

@end

@implementation JSONSerializer

+ (NSData *)serializeDictionaryToJSON:(NSDictionary *)dictionary {
    NSError *error = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                       options:0 // NSJSONWritingOptions
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Dictionary serialized to JSON data (%lu bytes)", (unsigned long)jsonData.length);
    return jsonData;
}

+ (NSString *)serializeDictionaryToJSONString:(NSDictionary *)dictionary {
    NSData *jsonData = [self serializeDictionaryToJSON:dictionary];

    if (!jsonData) {
        return nil;
    }

    NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                  encoding:NSUTF8StringEncoding];

    NSLog(@"JSON String: %@", jsonString);
    return jsonString;
}

+ (BOOL)writeDictionaryToJSONFile:(NSDictionary *)dictionary path:(NSString *)path {
    NSError *error = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return NO;
    }

    BOOL success = [jsonData writeToFile:path
                                 options:NSDataWritingAtomic
                                   error:&error];

    if (error) {
        NSLog(@"File write error: %@", error.localizedDescription);
        return NO;
    }

    NSLog(@"JSON written to file: %@", path);
    return success;
}

@end

// MARK: - 2. Serialize Array to JSON

@interface JSONArraySerializer : NSObject

+ (NSData *)serializeArrayToJSON:(NSArray *)array;
+ (NSString *)serializeArrayToJSONString:(NSArray *)array;
+ (NSData *)serializeArrayToPrettyJSON:(NSArray *)array;

@end

@implementation JSONArraySerializer

+ (NSData *)serializeArrayToJSON:(NSArray *)array {
    NSError *error = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
                                                       options:0
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Array serialized to JSON data");
    return jsonData;
}

+ (NSString *)serializeArrayToJSONString:(NSArray *)array {
    NSData *jsonData = [self serializeArrayToJSON:array];

    if (!jsonData) {
        return nil;
    }

    NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                  encoding:NSUTF8StringEncoding];

    NSLog(@"JSON Array String: %@", jsonString);
    return jsonString;
}

+ (NSData *)serializeArrayToPrettyJSON:(NSArray *)array {
    NSError *error = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Array serialized to pretty JSON");
    return jsonData;
}

@end

// MARK: - 3. Serialize Custom Object to JSON

@interface Person : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *email;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSArray *hobbies;

- (NSDictionary *)toDictionary;

@end

@implementation Person

- (NSDictionary *)toDictionary {
    return @{
        @"name": self.name ?: [NSNull null],
        @"email": self.email ?: [NSNull null],
        @"age": @(self.age),
        @"hobbies": self.hobbies ?: @[]
    };
}

@end

@interface CustomObjectSerializer : NSObject

+ (NSData *)serializePersonToJSON:(Person *)person;
+ (NSData *)serializePersonArrayToJSON:(NSArray *)people;

@end

@implementation CustomObjectSerializer

+ (NSData *)serializePersonToJSON:(Person *)person {
    NSDictionary *dict = [person toDictionary];

    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Person serialized to JSON");
    return jsonData;
}

+ (NSData *)serializePersonArrayToJSON:(NSArray *)people {
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:people.count];

    for (Person *person in people) {
        [array addObject:[person toDictionary]];
    }

    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Person array serialized to JSON");
    return jsonData;
}

@end

// MARK: - 4. Serialize Nested Objects

@interface Company : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *location;
@property (nonatomic, strong) NSArray *employees;

- (NSDictionary *)toDictionary;

@end

@implementation Company

- (NSDictionary *)toDictionary {
    NSMutableArray *employeeArray = [NSMutableArray array];

    for (Person *employee in self.employees) {
        [employeeArray addObject:[employee toDictionary]];
    }

    return @{
        @"name": self.name,
        @"location": self.location,
        @"employees": employeeArray
    };
}

@end

@interface NestedObjectSerializer : NSObject

+ (NSData *)serializeCompanyToJSON:(Company *)company;

@end

@implementation NestedObjectSerializer

+ (NSData *)serializeCompanyToJSON:(Company *)company {
    NSDictionary *dict = [company toDictionary];

    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    if (error) {
        NSLog(@"JSON serialization error: %@", error.localizedDescription);
        return nil;
    }

    NSLog(@"Company serialized to JSON");
    return jsonData;
}

@end

// MARK: - Main Demonstration

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"=== macOS Objective-C JSON Serialization Examples ===\n");

        // 1. Serialize dictionary
        NSLog(@"--- 1. Serialize Dictionary ---");

        NSDictionary *userDict = @{
            @"id": @(1),
            @"username": @"john_doe",
            @"email": @"john@example.com",
            @"active": @(YES),
            @"balance": @(1234.56)
        };

        [JSONSerializer serializeDictionaryToJSONString:userDict];

        NSString *jsonPath = @"/tmp/user.json";
        [JSONSerializer writeDictionaryToJSONFile:userDict path:jsonPath];

        // 2. Serialize array
        NSLog(@"\n--- 2. Serialize Array ---");

        NSArray *fruits = @[@"Apple", @"Banana", @"Orange", @"Mango"];
        [JSONArraySerializer serializeArrayToJSONString:fruits];

        NSArray *numbers = @[@1, @2, @3, @4, @5];
        [JSONArraySerializer serializeArrayToJSONString:numbers];

        // 3. Serialize custom object
        NSLog(@"\n--- 3. Serialize Custom Object ---");

        Person *person = [[Person alloc] init];
        person.name = @"Alice Smith";
        person.email = @"alice@example.com";
        person.age = 30;
        person.hobbies = @[@"Reading", @"Hiking", @"Photography"];

        NSData *personJSON = [CustomObjectSerializer serializePersonToJSON:person];
        if (personJSON) {
            NSString *jsonString = [[NSString alloc] initWithData:personJSON
                                                          encoding:NSUTF8StringEncoding];
            NSLog(@"Person JSON:\n%@", jsonString);
        }

        // 4. Serialize object array
        NSLog(@"\n--- 4. Serialize Object Array ---");

        Person *p1 = [[Person alloc] init];
        p1.name = @"Bob";
        p1.email = @"bob@example.com";
        p1.age = 25;
        p1.hobbies = @[@"Gaming"];

        Person *p2 = [[Person alloc] init];
        p2.name = @"Charlie";
        p2.email = @"charlie@example.com";
        p2.age = 35;
        p2.hobbies = @[@"Cooking", @"Traveling"];

        NSArray *people = @[p1, p2];
        NSData *peopleJSON = [CustomObjectSerializer serializePersonArrayToJSON:people];
        if (peopleJSON) {
            NSString *jsonString = [[NSString alloc] initWithData:peopleJSON
                                                          encoding:NSUTF8StringEncoding];
            NSLog(@"People Array JSON:\n%@", jsonString);
        }

        // 5. Serialize nested objects
        NSLog(@"\n--- 5. Serialize Nested Objects ---");

        Company *company = [[Company alloc] init];
        company.name = @"Tech Corp";
        company.location = @"San Francisco, CA";

        Person *emp1 = [[Person alloc] init];
        emp1.name = @"David";
        emp1.email = @"david@techcorp.com";
        emp1.age = 28;
        emp1.hobbies = @[@"Coding"];

        Person *emp2 = [[Person alloc] init];
        emp2.name = @"Emma";
        emp2.email = @"emma@techcorp.com";
        emp2.age = 32;
        emp2.hobbies = @[@"Design", @"Music"];

        company.employees = @[emp1, emp2];

        NSData *companyJSON = [NestedObjectSerializer serializeCompanyToJSON:company];
        if (companyJSON) {
            NSString *jsonString = [[NSString alloc] initWithData:companyJSON
                                                          encoding:NSUTF8StringEncoding];
            NSLog(@"Company JSON:\n%@", jsonString);
        }

        NSLog(@"\n=== JSON Serialization Examples Completed ===");
    }

    return 0;
}
CódigoobjectivecIntermediário

Desserialização JSON

Analisar strings JSON e converter em objetos Objective-C usando NSJSONSerialization

Dificuldade

5/10

Tempo estimado

25 min

Etiquetas

objectivec, macos, serialization, json

Pré-requisitos

Objective-C basics, Foundation framework

objectivec
// macOS Objective-C JSON Deserialization Examples
// Using NSJSONSerialization for JSON decoding

#import <Foundation/Foundation.h>

// MARK: - 1. Deserialize JSON to Dictionary

@interface JSONDeserializer : NSObject

+ (NSDictionary *)deserializeJSONToDictionary:(NSData *)jsonData;
+ (NSDictionary *)deserializeJSONStringToDictionary:(NSString *)jsonString;
+ (NSDictionary *)readJSONFileToDictionary:(NSString *)path;

@end

@implementation JSONDeserializer

+ (NSDictionary *)deserializeJSONToDictionary:(NSData *)jsonData {
    NSError *error = nil;

    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:0
                                                      error:&error];

    if (error) {
        NSLog(@"JSON deserialization error: %@", error.localizedDescription);
        return nil;
    }

    if (![jsonObject isKindOfClass:[NSDictionary class]]) {
        NSLog(@"JSON is not a dictionary");
        return nil;
    }

    NSLog(@"JSON deserialized to dictionary");
    return (NSDictionary *)jsonObject;
}

+ (NSDictionary *)deserializeJSONStringToDictionary:(NSString *)jsonString {
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

    if (!jsonData) {
        NSLog(@"Failed to convert string to data");
        return nil;
    }

    return [self deserializeJSONToDictionary:jsonData];
}

+ (NSDictionary *)readJSONFileToDictionary:(NSString *)path {
    NSError *error = nil;

    NSData *jsonData = [NSData dataWithContentsOfFile:path
                                              options:0
                                                error:&error];

    if (error) {
        NSLog(@"File read error: %@", error.localizedDescription);
        return nil;
    }

    return [self deserializeJSONToDictionary:jsonData];
}

@end

// MARK: - 2. Deserialize JSON to Array

@interface JSONArrayDeserializer : NSObject

+ (NSArray *)deserializeJSONToArray:(NSData *)jsonData;
+ (NSArray *)deserializeJSONStringToArray:(NSString *)jsonString;

@end

@implementation JSONArrayDeserializer

+ (NSArray *)deserializeJSONToArray:(NSData *)jsonData {
    NSError *error = nil;

    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:0
                                                      error:&error];

    if (error) {
        NSLog(@"JSON deserialization error: %@", error.localizedDescription);
        return nil;
    }

    if (![jsonObject isKindOfClass:[NSArray class]]) {
        NSLog(@"JSON is not an array");
        return nil;
    }

    NSLog(@"JSON deserialized to array");
    return (NSArray *)jsonObject;
}

+ (NSArray *)deserializeJSONStringToArray:(NSString *)jsonString {
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

    if (!jsonData) {
        NSLog(@"Failed to convert string to data");
        return nil;
    }

    return [self deserializeJSONToArray:jsonData];
}

@end

// MARK: - 3. Deserialize to Custom Object

@interface Person : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *email;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSArray *hobbies;

+ (instancetype)fromDictionary:(NSDictionary *)dict;
- (NSString *)description;

@end

@implementation Person

+ (instancetype)fromDictionary:(NSDictionary *)dict {
    Person *person = [[Person alloc] init];

    person.name = dict[@"name"];
    person.email = dict[@"email"];

    id ageValue = dict[@"age"];
    if ([ageValue isKindOfClass:[NSNumber class]]) {
        person.age = [ageValue integerValue];
    }

    id hobbiesValue = dict[@"hobbies"];
    if ([hobbiesValue isKindOfClass:[NSArray class]]) {
        person.hobbies = (NSArray *)hobbiesValue;
    } else {
        person.hobbies = @[];
    }

    return person;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"Person[name=%@, email=%@, age=%ld, hobbies=%@]",
            self.name, self.email, (long)self.age, self.hobbies];
}

@end

@interface CustomObjectDeserializer : NSObject

+ (Person *)deserializePersonFromJSON:(NSData *)jsonData;
+ (NSArray *)deserializePersonArrayFromJSON:(NSData *)jsonData;

@end

@implementation CustomObjectDeserializer

+ (Person *)deserializePersonFromJSON:(NSData *)jsonData {
    NSError *error = nil;

    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:0
                                                      error:&error];

    if (error) {
        NSLog(@"JSON deserialization error: %@", error.localizedDescription);
        return nil;
    }

    if (![jsonObject isKindOfClass:[NSDictionary class]]) {
        NSLog(@"JSON is not a dictionary");
        return nil;
    }

    Person *person = [Person fromDictionary:(NSDictionary *)jsonObject];
    NSLog(@"Person deserialized from JSON");

    return person;
}

+ (NSArray *)deserializePersonArrayFromJSON:(NSData *)jsonData {
    NSError *error = nil;

    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:0
                                                      error:&error];

    if (error) {
        NSLog(@"JSON deserialization error: %@", error.localizedDescription);
        return nil;
    }

    if (![jsonObject isKindOfClass:[NSArray class]]) {
        NSLog(@"JSON is not an array");
        return nil;
    }

    NSArray *jsonArray = (NSArray *)jsonObject;
    NSMutableArray *people = [NSMutableArray arrayWithCapacity:jsonArray.count];

    for (NSDictionary *dict in jsonArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [people addObject:[Person fromDictionary:dict]];
        }
    }

    NSLog(@"Person array deserialized from JSON (%lu people)", (unsigned long)people.count);
    return [people copy];
}

@end

// MARK: - 4. Handle Invalid JSON

@interface JSONValidator : NSObject

+ (BOOL)isValidJSON:(NSData *)jsonData;
+ (NSString *)getValidationError:(NSData *)jsonData;

@end

@implementation JSONValidator

+ (BOOL)isValidJSON:(NSData *)jsonData {
    NSError *error = nil;

    [NSJSONSerialization JSONObjectWithData:jsonData
                                    options:0
                                      error:&error];

    return error == nil;
}

+ (NSString *)getValidationError:(NSData *)jsonData {
    NSError *error = nil;

    [NSJSONSerialization JSONObjectWithData:jsonData
                                    options:0
                                      error:&error];

    return error ? error.localizedDescription : nil;
}

@end

// MARK: - 5. Parse Mixed JSON Structures

@interface MixedJSONParser : NSObject

+ (void)parseMixedJSON:(NSData *)jsonData;

@end

@implementation MixedJSONParser

+ (void)parseMixedJSON:(NSData *)jsonData {
    NSError *error = nil;

    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:0
                                                      error:&error];

    if (error) {
        NSLog(@"JSON parsing error: %@", error.localizedDescription);
        return;
    }

    if ([jsonObject isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *)jsonObject;
        NSLog(@"Parsed dictionary with %lu keys", (unsigned long)dict.count);

        // Iterate through dictionary
        [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            NSLog(@"  Key: %@, Value: %@", key, obj);

            if ([obj isKindOfClass:[NSArray class]]) {
                NSLog(@"    -> Array with %lu items", (unsigned long)[obj count]);
            } else if ([obj isKindOfClass:[NSDictionary class]]) {
                NSLog(@"    -> Nested dictionary");
            }
        }];

    } else if ([jsonObject isKindOfClass:[NSArray class]]) {
        NSArray *array = (NSArray *)jsonObject;
        NSLog(@"Parsed array with %lu items", (unsigned long)array.count);
    }
}

@end

// MARK: - Main Demonstration

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"=== macOS Objective-C JSON Deserialization Examples ===\n");

        // 1. Deserialize to dictionary
        NSLog(@"--- 1. Deserialize to Dictionary ---");

        NSString *userJSONString = @"{\"id\": 1,\"username\": \"john_doe\", \"email\": \"john@example.com\", \"active\": true}";

        NSDictionary *userDict = [JSONDeserializer deserializeJSONStringToDictionary:userJSONString];
        if (userDict) {
            NSLog(@"Username: %@", userDict[@"username"]);
            NSLog(@"Email: %@", userDict[@"email"]);
            NSLog(@"Active: %@", userDict[@"active"]);
        }

        // 2. Deserialize to array
        NSLog(@"\n--- 2. Deserialize to Array ---");

        NSString *fruitsJSONString = @"[\"Apple\", \"Banana\", \"Orange\", \"Mango\"]";

        NSArray *fruits = [JSONArrayDeserializer deserializeJSONStringToArray:fruitsJSONString];
        if (fruits) {
            NSLog(@"Fruits: %@", fruits);
        }

        // 3. Deserialize to custom object
        NSLog(@"\n--- 3. Deserialize to Custom Object ---");

        NSString *personJSONString = @"{\"name\": \"Alice Smith\", \"email\": \"alice@example.com\", \"age\": 30, \"hobbies\": [\"Reading\", \"Hiking\"]}";

        NSData *personJSONData = [personJSONString dataUsingEncoding:NSUTF8StringEncoding];
        Person *person = [CustomObjectDeserializer deserializePersonFromJSON:personJSONData];
        if (person) {
            NSLog(@"%@", person);
        }

        // 4. Deserialize to object array
        NSLog(@"\n--- 4. Deserialize to Object Array ---");

        NSString *peopleJSONString = @"["
            @"{\"name\": \"Bob\", \"email\": \"bob@example.com\", \"age\": 25, \"hobbies\": [\"Gaming\"]},"
            @"{\"name\": \"Charlie\", \"email\": \"charlie@example.com\", \"age\": 35, \"hobbies\": [\"Cooking\", \"Traveling\"]}"
        @"]";

        NSData *peopleJSONData = [peopleJSONString dataUsingEncoding:NSUTF8StringEncoding];
        NSArray *people = [CustomObjectDeserializer deserializePersonArrayFromJSON:peopleJSONData];
        for (Person *p in people) {
            NSLog(@"%@", p);
        }

        // 5. Validate JSON
        NSLog(@"\n--- 5. Validate JSON ---");

        NSString *validJSON = @"{\"name\": \"Test\", \"value\": 123}";
        NSString *invalidJSON = @"{\"name\": \"Test\", \"value\": 123";

        NSData *validData = [validJSON dataUsingEncoding:NSUTF8StringEncoding];
        NSData *invalidData = [invalidJSON dataUsingEncoding:NSUTF8StringEncoding];

        NSLog(@"Valid JSON: %d", [JSONValidator isValidJSON:validData]);
        NSLog(@"Invalid JSON: %d", [JSONValidator isValidJSON:invalidData]);

        NSString *error = [JSONValidator getValidationError:invalidData];
        if (error) {
            NSLog(@"Error: %@", error);
        }

        // 6. Parse mixed structures
        NSLog(@"\n--- 6. Parse Mixed Structures ---");

        NSString *mixedJSON = @"{"
            @"\"company\": \"Tech Corp\","
            @"\"employees\": ["
            @"  {\"name\": \"Alice\", \"role\": \"Engineer\"},"
            @"  {\"name\": \"Bob\", \"role\": \"Designer\"}"
            @"],"
            @"\"metadata\": {"
            @"   \"founded\": 2010,"
            @"   \"location\": \"San Francisco\""
            @"}"
        @"}";

        NSData *mixedData = [mixedJSON dataUsingEncoding:NSUTF8StringEncoding];
        [MixedJSONParser parseMixedJSON:mixedData];

        NSLog(@"\n=== JSON Deserialization Examples Completed ===");
    }

    return 0;
}
CódigoobjectivecIntermediário

Análise XML

Analisar documentos XML usando NSXMLParser com padrão delegado

Dificuldade

6/10

Tempo estimado

30 min

Etiquetas

objectivec, macos, serialization, xml

Pré-requisitos

Objective-C basics, Foundation framework, Delegate pattern

objectivec
// macOS Objective-C XML Parsing Examples
// Using NSXMLParser delegate pattern

#import <Foundation/Foundation.h>

// MARK: - 1. Simple XML Parser

@interface SimpleXMLParser : NSObject <NSXMLParserDelegate>

@property (nonatomic, strong) NSMutableString *currentElementValue;
@property (nonatomic, strong) NSMutableArray *parsedItems;
@property (nonatomic, strong) NSMutableDictionary *currentItem;

- (void)parseXMLFromString:(NSString *)xmlString;
- (void)parseXMLFromFile:(NSString *)filePath;

@end

@implementation SimpleXMLParser

- (instancetype)init {
    self = [super init];

    if (self) {
        _currentElementValue = [[NSMutableString alloc] init];
        _parsedItems = [[NSMutableArray alloc] init];
    }

    return self;
}

- (void)parseXMLFromString:(NSString *)xmlString {
    NSData *xmlData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
    parser.delegate = self;

    BOOL success = [parser parse];

    if (success) {
        NSLog(@"XML parsing successful, parsed %lu items", (unsigned long)self.parsedItems.count);
    } else {
        NSLog(@"XML parsing failed: %@", parser.parserError.localizedDescription);
    }
}

- (void)parseXMLFromFile:(NSString *)filePath {
    NSData *xmlData = [NSData dataWithContentsOfFile:filePath];

    if (!xmlData) {
        NSLog(@"Failed to read XML file");
        return;
    }

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
    parser.delegate = self;

    BOOL success = [parser parse];

    if (success) {
        NSLog(@"XML parsing successful, parsed %lu items", (unsigned long)self.parsedItems.count);
    } else {
        NSLog(@"XML parsing failed: %@", parser.parserError.localizedDescription);
    }
}

// NSXMLParserDelegate methods

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
    attributes:(NSDictionary<NSString *, NSString *> *)attributeDict {

    [self.currentElementValue setString:@""];

    if ([elementName isEqualToString:@"item"]) {
        self.currentItem = [[NSMutableDictionary alloc] init];
    }

    NSLog(@"Started element: %@", elementName);

    // Log attributes
    [attributeDict enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
        NSLog(@"  Attribute: %@ = %@", key, value);
    }];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    [self.currentElementValue appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName {

    NSString *trimmedValue = [self.currentElementValue stringByTrimmingCharactersInSet:
                             [NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if ([elementName isEqualToString:@"item"]) {
        if (self.currentItem) {
            [self.parsedItems addObject:self.currentItem];
            self.currentItem = nil;
        }
    } else if (self.currentItem && trimmedValue.length > 0) {
        [self.currentItem setObject:trimmedValue forKey:elementName];
    }

    NSLog(@"Ended element: %@ = %@", elementName, trimmedValue);
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"XML document parsing completed");
    NSLog(@"Parsed items: %@", self.parsedItems);
}

@end

// MARK: - 2. Catalog XML Parser

@interface Book : NSObject

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *author;
@property (nonatomic, strong) NSString *genre;
@property (nonatomic, assign) CGFloat price;
@property (nonatomic, assign) NSInteger publishDate;
@property (nonatomic, strong) NSString *description;

- (NSString *)description;

@end

@implementation Book

- (NSString *)description {
    return [NSString stringWithFormat:@"Book[title=%@, author=%@, genre=%@, price=%.2f, year=%ld]",
            self.title, self.author, self.genre, self.price, (long)self.publishDate];
}

@end

@interface CatalogXMLParser : NSObject <NSXMLParserDelegate>

@property (nonatomic, strong) NSMutableArray *books;
@property (nonatomic, strong) Book *currentBook;
@property (nonatomic, strong) NSMutableString *currentText;

- (NSArray *)parseCatalogXML:(NSString *)xmlString;

@end

@implementation CatalogXMLParser

- (instancetype)init {
    self = [super init];

    if (self) {
        _books = [[NSMutableArray alloc] init];
        _currentText = [[NSMutableString alloc] init];
    }

    return self;
}

- (NSArray *)parseCatalogXML:(NSString *)xmlString {
    NSData *xmlData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
    parser.delegate = self;

    if ([parser parse]) {
        NSLog(@"Catalog parsed successfully, %lu books", (unsigned long)self.books.count);
        return [self.books copy];
    }

    NSLog(@"Parsing failed: %@", parser.parserError.localizedDescription);
    return nil;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict {

    [self.currentText setString:@""];

    if ([elementName isEqualToString:@"book"]) {
        self.currentBook = [[Book alloc] init];

        // Extract attributes from book element
        NSString *idValue = attributeDict[@"id"];
        NSLog(@"Parsing book with ID: %@", idValue);
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    [self.currentText appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName {

    NSString *trimmedText = [self.currentText stringByTrimmingCharactersInSet:
                            [NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if ([elementName isEqualToString:@"book"]) {
        if (self.currentBook) {
            [self.books addObject:self.currentBook];
            self.currentBook = nil;
        }
    } else if (self.currentBook) {
        if ([elementName isEqualToString:@"title"]) {
            self.currentBook.title = trimmedText;
        } else if ([elementName isEqualToString:@"author"]) {
            self.currentBook.author = trimmedText;
        } else if ([elementName isEqualToString:@"genre"]) {
            self.currentBook.genre = trimmedText;
        } else if ([elementName isEqualToString:@"price"]) {
            self.currentBook.price = [trimmedText floatValue];
        } else if ([elementName isEqualToString:@"publish_date"]) {
            // Parse year from date format
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyy-MM-dd";
            NSDate *date = [formatter dateFromString:trimmedText];

            if (date) {
                NSCalendar *calendar = [NSCalendar currentCalendar];
                NSDateComponents *components = [calendar components:NSCalendarUnitYear
                                                            fromDate:date];
                self.currentBook.publishDate = components.year;
            }
        } else if ([elementName isEqualToString:@"description"]) {
            self.currentBook.description = trimmedText;
        }
    }
}

@end

// MARK: - 3. RSS Feed Parser

@interface RSSFeedParser : NSObject <NSXMLParserDelegate>

@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic, strong) NSMutableDictionary *currentItem;
@property (nonatomic, strong) NSMutableString *currentElementValue;
@property (nonatomic, strong) NSMutableString *currentElementName;

@property (nonatomic, strong) NSString *feedTitle;
@property (nonatomic, strong) NSString *feedDescription;
@property (nonatomic, strong) NSString *feedLink;

- (NSDictionary *)parseRSSFeed:(NSString *)rssString;

@end

@implementation RSSFeedParser

- (instancetype)init {
    self = [super init];

    if (self) {
        _items = [[NSMutableArray alloc] init];
        _currentElementValue = [[NSMutableString alloc] init];
        _currentElementName = [[NSMutableString alloc] init];
    }

    return self;
}

- (NSDictionary *)parseRSSFeed:(NSString *)rssString {
    NSData *rssData = [rssString dataUsingEncoding:NSUTF8StringEncoding];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:rssData];
    parser.delegate = self;

    if ([parser parse]) {
        NSLog(@"RSS feed parsed, %lu items", (unsigned long)self.items.count);

        return @{
            @"title": self.feedTitle ?: @"",
            @"description": self.feedDescription ?: @"",
            @"link": self.feedLink ?: @"",
            @"items": [self.items copy]
        };
    }

    return nil;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict {

    [self.currentElementName setString:elementName];
    [self.currentElementValue setString:@""];

    if ([elementName isEqualToString:@"item"]) {
        self.currentItem = [[NSMutableDictionary alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    [self.currentElementValue appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName {

    NSString *value = [self.currentElementValue stringByTrimmingCharactersInSet:
                      [NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if ([elementName isEqualToString:@"item"]) {
        if (self.currentItem) {
            [self.items addObject:self.currentItem];
            self.currentItem = nil;
        }
    } else if ([elementName isEqualToString:@"title"] && !self.currentItem) {
        self.feedTitle = value;
    } else if ([elementName isEqualToString:@"description"] && !self.currentItem) {
        self.feedDescription = value;
    } else if ([elementName isEqualToString:@"link"] && !self.currentItem) {
        self.feedLink = value;
    } else if (self.currentItem && value.length > 0) {
        [self.currentItem setObject:value forKey:elementName];
    }
}

@end

// MARK: - Main Demonstration

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"=== macOS Objective-C XML Parsing Examples ===\n");

        // 1. Simple XML parsing
        NSLog(@"--- 1. Simple XML Parsing ---");

        NSString *simpleXML = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
            @"<data>"
            @"  <item>"
            @"    <name>Apple</name>"
            @"    <price>1.50</price>"
            @"    <quantity>10</quantity>"
            @"  </item>"
            @"  <item>"
            @"    <name>Banana</name>"
            @"    <price>0.75</price>"
            @"    <quantity>20</quantity>"
            @"  </item>"
            @"</data>";

        SimpleXMLParser *simpleParser = [[SimpleXMLParser alloc] init];
        [simpleParser parseXMLFromString:simpleXML];

        // 2. Catalog XML parsing
        NSLog(@"\n--- 2. Catalog XML Parsing ---");

        NSString *catalogXML = @"<?xml version=\"1.0\"?>"
            @"<catalog>"
            @"  <book id=\"bk101\">"
            @"    <author>Gambardella, Matthew</author>"
            @"    <title>XML Developer's Guide</title>"
            @"    <genre>Computer</genre>"
            @"    <price>44.95</price>"
            @"    <publish_date>2000-10-01</publish_date>"
            @"    <description>An in-depth look at creating applications with XML.</description>"
            @"  </book>"
            @"  <book id=\"bk102\">"
            @"    <author>Ralls, Kim</author>"
            @"    <title>Midnight Rain</title>"
            @"    <genre>Fantasy</genre>"
            @"    <price>5.95</price>"
            @"    <publish_date>2000-12-16</publish_date>"
            @"    <description>A former architect battles corporate zombies.</description>"
            @"  </book>"
            @"</catalog>";

        CatalogXMLParser *catalogParser = [[CatalogXMLParser alloc] init];
        NSArray *books = [catalogParser parseCatalogXML:catalogXML];

        for (Book *book in books) {
            NSLog(@"%@", book);
        }

        // 3. RSS feed parsing
        NSLog(@"\n--- 3. RSS Feed Parsing ---");

        NSString *rssFeed = @"<?xml version=\"1.0\"?>"
            @"<rss version=\"2.0\">"
            @"  <channel>"
            @"    <title>Tech News</title>"
            @"    <description>Latest technology news</description>"
            @"    <link>https://example.com</link>"
            @"    <item>"
            @"      <title>New iPhone Released</title>"
            @"      <link>https://example.com/iphone</link>"
            @"      <description>Apple announces latest iPhone model</description>"
            @"      <pubDate>Mon, 25 Dec 2025 10:00:00 GMT</pubDate>"
            @"    </item>"
            @"    <item>"
            @"      <title>AI Breakthrough</title>"
            @"      <link>https://example.com/ai</link>"
            @"      <description>Scientists achieve major AI milestone</description>"
            @"      <pubDate>Sun, 24 Dec 2025 15:30:00 GMT</pubDate>"
            @"    </item>"
            @"  </channel>"
            @"</rss>";

        RSSFeedParser *rssParser = [[RSSFeedParser alloc] init];
        NSDictionary *feed = [rssParser parseRSSFeed:rssFeed];

        NSLog(@"Feed Title: %@", feed[@"title"]);
        NSLog(@"Feed Description: %@", feed[@"description"]);
        NSLog(@"\nFeed Items:");

        for (NSDictionary *item in feed[@"items"]) {
            NSLog(@"  - %@ (%@)", item[@"title"], item[@"pubDate"]);
        }

        NSLog(@"\n=== XML Parsing Examples Completed ===");
    }

    return 0;
}

Ferramentas

Ferramentas frequentemente usadas com este exemplo

Relacionado

Coleções de exemplos relacionadas

Relacionado

Temas relacionados

Tema

Revisão e entrega de um componente de sorteio auto-hospedado

Revise configuração, exemplos de respostas, legibilidade e visualização responsiva antes de entregar os arquivos e as evidências de integração do sorteio.

Tema

Modelos farmacocineticos didaticos e checagem de resultados

Explore formulas farmacocineticas, hipoteses de modelo e consistencia de resultados para ensino ou QA, sem doseamento ou conselho clinico.

Tema

Ferramentas de cifras clássicas e modernas

Pratique cifras com segurança em contextos educacionais ou CTF, comparando transformações clássicas reversíveis com criptografia moderna e seus limites.

Tema

Geradores de JSON para codigo tipado

Gere modelos tipados a partir de JSON ou schemas para TypeScript, Go, Rust, Kotlin, Java, C# e validacao Zod.

Início
Ferramentas3212
Exemplos593
Visualizações471
Visualizadores de arquivos103
Temas239
Etiquetas333
Imagens, áudio e vídeo657

Ferramentas de processamento, conversão e otimização de imagens, áudio e vídeo

Matemática, datas e finanças647

Calculadoras, ferramentas numéricas, lógica de datas, estatística e finanças

Design e cor298

Ferramentas de cor, layout, gráficos, estilo visual e apoio ao design

Texto e escrita209

Ferramentas de escrita, limpeza, formatação, extração e análise de texto

Desenvolvimento e Web193

Utilitários para desenvolvedores, rede, depuração web e automação

Conversão e codificação175

Ferramentas de conversão de formatos, arquivos, unidades, arquivos compactados e codificação

Lançamento de quiz de captação e transferência de contatos
Textos memoriais: revisão familiar e entrega para impressão
Hábitos privados: backup, restauração e revisão para partilha
De artigo a rascunhos de X e LinkedIn: revisão factual e entrega
Revisão de aula Markdown interativa e entrega offline

© 2026 Elysia Tools. Todos os direitos reservados.

DocumentaçãoPreçosSobreTermos de serviçoPolítica de privacidadeCookiesContato

Format Conversion

Conversor XML para JSON

Converte dados XML para formato JSON com opções de análise personalizáveis

Development

Visualizador de saves de jogos

Solte qualquer arquivo de save — NBT de Minecraft, GVAS de Unreal Engine, contêineres de Palworld, saves de texto JSON/XML/INI/Lua, perfis SQLite — e receba o formato detectado, versões da engine, campos-chave de progresso e uma árvore estrutural limitada.

Format Conversion

Conversor JSON para XML

Converte dados JSON para formato XML com opções de formato personalizáveis

Development

Auditor de política de licenças SBOM SPDX / CycloneDX

Audita licenças SPDX 2.3 ou CycloneDX 1.5 e gera SARIF com caminhos de dependência

Format Conversion

Injetor XLSX de Sparklines

Insere sparklines em celulas Excel via patch XML da planilha

Data Processing

JSON Key Extractor

Extract all keys from JSON objects with multiple output formats. Perfect for analyzing JSON structure, documentation generation, and understanding complex nested objects.

SEO

Gerador de JSON-LD a partir de CSV

Transforma linhas de CSV ou Excel em JSON-LD Schema.org para artigos, produtos ou eventos com saida pronta para validacao de SEO

Data Analysis

Detector de anomalias em series temporais

Envia dados de serie temporal em CSV ou JSON, detecta anomalias com Z-Score e IQR e retorna um relatorio com grafico

C++

Exemplos de Serialização Windows C++

Exemplos de serialização de dados Windows C++ incluindo serialização/deserialização JSON e análise XML

Java

Exemplos de Serialização Android Java

Exemplos de serialização Android Java incluindo serialização JSON, desserialização e análise XML

Kotlin

Exemplos de Serialização Android Kotlin

Exemplos de serialização Android Kotlin incluindo serialização JSON, desserialização e análise XML

Go

Exemplos de Serialização de Dados Web Go

Exemplos de serialização de dados Web Go incluindo codificação/decodificação JSON e análise XML

Python

Exemplos de Serialização Web Python

Exemplos de serialização Web Python incluindo processamento JSON e XML

Rust

Exemplos de Serialização de Dados Web Rust

Exemplos de serialização de dados Web Rust incluindo serialização JSON, desserialização e análise XML