XML to NSDictionary Converter

I found recently a nice lib easily converting XML to NSDictionary. The code is now on GitHub.

Here are the key ideas:

  1. XML elements map to key names in the dictionary
  2. Each element corresponds to a child dictionary
  3. Attribute key-value pairs are added to the element’s child dictionary
  4. Strings from text nodes are assigned to the child dictionary’s “text” key
  5. If an element name is encountered multiple times, the value of the element is set to an array of children dictionaries

For a more detailed presentation.

A detailed example:

The XML to parse:

//    <items>
//        <item id=”0001″ type=”donut”>
//            <name>Cake</name>
//            <ppu>0.55</ppu>
//            <batters>
//                <batter id=”1001″>Regular</batter>
//                <batter id=”1002″>Chocolate</batter>
//                <batter id=”1003″>Blueberry</batter>
//            </batters>
//            <topping id=”5001″>None</topping>
//            <topping id=”5002″>Glazed</topping>
//            <topping id=”5005″>Sugar</topping>
//        </item>
//    </items>
//

The Code:

NSString *testXMLString = @”<items><item id=\”0001\” type=\”donut\”><name>Cake</name><ppu>0.55</ppu><batters><batter id=\”1001\”>Regular</batter><batter id=\”1002\”>Chocolate</batter><batter id=\”1003\”>Blueberry</batter></batters><topping id=\”5001\”>None</topping><topping id=\”5002\”>Glazed</topping><topping id=\”5005\”>Sugar</topping></item></items>”;

// Parse the XML into a dictionary
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];

// Print the dictionary
NSLog(@”%@”, xmlDictionary);

The result

// xmlDictionary = {
//    items = {
//        item = {
//            id = 0001;
//            type = donut;
//            name = {
//                text = Cake;
//            };
//            ppu = {
//                text = 0.55;
//            };
//            batters = {
//                batter = (
//                    {
//                        id = 1001;
//                        text = Regular;
//                    },
//                    {
//                        id = 1002;
//                        text = Chocolate;
//                    },
//                    {
//                        id = 1003;
//                        text = Blueberry;
//                    }
//                );
//            };
//            topping = (
//                {
//                    id = 5001;
//                    text = None;
//                },
//                {
//                    id = 5002;
//                    text = Glazed;
//                },
//                {
//                    id = 5005;
//                    text = Sugar;
//                }
//            );
//        };
//     };
// }
//

Killer tool to develop on iPhone: ack-grep

When developing for iPhone, the IDE coming in mind is XCode and it’s a good thing cause XCode is a good piece of software. I use XCode everyday but I never found the search functionality good enough. That’s why I switched to ack-grep. It’s a little perl script way faster than grep or any other search functionality. And the result presentation is clear and efficient.

Recently, I had to work on an complex iPhone application developed by another company. ack-grep helped me a lot to dig in the hairy code. Since I’m using this tool I can’t work without it.

The installation is easy, it’s worth a try.