XML Generation in Objective-C with XMLDocument

Posted by – October 21, 2011

XML generation is one of the most common programming tasks done by several developers around the world, with XML we can easily integrate different platforms, using a data format that is very easy to understand and work with.

Unfortunately iOS API doesn’t provide a built in class that help us with XML generation, so I decided to write my own helper class for this purpose.

XMLDocument is a very simple class, you may not found several features that are very common in XML generation, but with this class I could easily implement what I need to get one of my iOS project done.

Here’s how the class is implemented, any help on improvements will be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#import "XMLDocument.h"


@implementation XMLDocument

#pragma mark -
#pragma mark Initialization methods

- (id) init {
    self = [super init];
    data = [[NSMutableData alloc] init];
    [data appendData:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" dataUsingEncoding:NSUTF8StringEncoding]];
    attributes = [[NSMutableDictionary alloc] init];
   
    return self;
}

#pragma mark -
#pragma mark XMLDocument methods

- (void) startTag:(NSString*) aTagName andAcceptAttributes:(BOOL) acceptAttributes {
    if (acceptAttributes)
        [data appendData:[[NSString stringWithFormat:@"<%@ ", aTagName] dataUsingEncoding:NSUTF8StringEncoding]];
    else
        [data appendData:[[NSString stringWithFormat:@"<%@>", aTagName] dataUsingEncoding:NSUTF8StringEncoding]];

    [attributes removeAllObjects];
}

- (void) addAttribute:(NSString*) aName withValue:(NSString *) aValue {
    [attributes setValue:aValue forKey:aName];
}

- (void) writeAttributesOnOpenTag {
    [self writeAttributesOnOpenTagAndSelfCloseTag:NO]
}

- (void) writeAttributesOnOpenTagAndSelfCloseTag:(BOOL)selfCloseTag {
    for (id key in attributes)
        [data appendData:[[NSString stringWithFormat:@" %@=\"%@\"", key, [attributes objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];

    if (selfCloseTag)
        [data appendData:[@"/>" dataUsingEncoding:NSUTF8StringEncoding]];
    else   
        [data appendData:[@">" dataUsingEncoding:NSUTF8StringEncoding]];
}

- (void) closeTag:(NSString*) aTagName {
    [data appendData:[[NSString stringWithFormat:@"</%@>", aTagName] dataUsingEncoding:NSUTF8StringEncoding]];
}

- (NSMutableData *) data {
    return data;
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {  
    [attributes release];
    [data release];
    [super dealloc];
}
@end
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>