Super easy iOS XML parsing

I just love it when you need something simple and easy that perfectly suits your needs. I needed some lightweight XML parser for iOS, and I found it.

To be honest (since I’m coming from java background), I didn’t quite fall in love with objective-c at first, and it seemed to be that I needed a lot more code to make some basic stuff work. For example XML parsing. NSXMLParser is ok, but it’s event driven (link #1, link #2). I wanted some dead simple DOM parser.

So the whole library is just two classes SMXMLElement and SMXMLDocument. To show you how simple it is to parse a file, here is a (reduced) snippet from my application:


- (void) parseData:(NSData *) {

   NSError *error;
   SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];

   // check for errors
   if (error) {
   DDLogError(@"Error while parsing the document: %@", error);
   return nil;
}

// Array of objects that we are returning
NSMutableArray *result = [[NSMutableArray alloc] init];

// Get the videos node
SMXMLElement *videos = document.root;

// Go through every sub-element "video"
for (SMXMLElement *video in [videos childrenNamed:@"video"]) {
   VideoInfo *info = [[VideoInfo alloc] init];
   info.name = ;

   // Get other values from XML...
   [result addObject:info];
   [info release];
}

return result;
}

A shorter version of XML that the code above parses is:


<videos>
   <video>
      <name>Yup, this a name of a video</name>
   </video>
   <video>
      <name>And another one :)</name>
   </video>
</videos>

As you can see I’m only using (NSString*) valueWithPath:(NSString *) here, but there are a couple more


- (SMXMLElement *)childNamed:(NSString *)name;
- (NSArray *)childrenNamed:(NSString *)name;
- (SMXMLElement *)childWithAttribute:(NSString *)attributeName  value:(NSString *)attributeValue;
- (NSString *)attributeNamed:(NSString *)name;
- (SMXMLElement *)descendantWithPath:(NSString *)path;
- (NSString *)valueWithPath:(NSString *)path;

Do you really need anything else (in some small applications)? :)
Really awesome, kudos to the creators.

The Unbearable Lightness of Java – jodd

I was amazed when I first read about this library.  It’s almost unbelievable what is packed in less than 850KB. Without further ado, let me represent jodd!

jodd logo

As the author said himself:

Jodd = tools + ioc + mvc + db + aop + tx < 850KB

There are two main parts, one is the general utility library, and the second being a set of lightweightapplication frameworks.

  1. How many times you had to deal with date in java? You can still feel the pain? Here is the cure JDateTime. Ever had a need to copy entire bean? Use BeanUtil. Sending email is your thing? Yup, jodd has that covered. Auto-populating forms? Checked. Also there are StringUtil, Printf, special JSP tag library and bunch more. Very useful from time to time.
  2. Second part is interesting as well. There is a slick MVC framework named Madvoc (I’ve used it in one of my projects, simple as hell), Petite (DI container, no XMLs :)), DB access layer and more.

Check it up yourself at www.jodd.org. Documentation is great and there are planty of examples to get you started (much better than my examples anyway)

Also there you can find “Uphea“, application built on jodd framework.