3

I want to parse a very simple XML string into an NSDictionary so that I can easily access the attributes.

RecurrenceInfo: "<RecurrenceInfo Start="12/24/2013 01:00:00" End="01/03/2014 01:00:00" DayNumber="24" WeekOfMonth="0" WeekDays="62" Id="49409301-c3ec-43f0-8571-ca42258e8a6f" Month="12" OccurrenceCount="9" Range="1" />",

I was looking around and found some libraries, but then I still need to implement lot of things. Anybody know if this can be done more easy ?

kind regards

Steaphann
  • 2,797
  • 6
  • 50
  • 109

3 Answers3

5

There is a good wrapper class written on top NSXMLParser. Just pass the XML String. It will convert into NSDictionary.

https://github.com/amarcadet/XMLReader

Mavericks
  • 524
  • 3
  • 5
1

Don't use a third party library. Use one of the built in ones:

  • NSXMLParser
  • libxml2

For this case, you'd be better off using NSXMLParser, there are plenty of examples available, but the official documentation is at: https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/XMLParsing/XMLParsing.html#//apple_ref/doc/uid/10000186i

You should create a class of your own specifically to parse your type of XML string using NSXMLParser. It could return an instance of itself or perhaps an NSDictionary. Up to you.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
1

Create the object of NSXMLParser.

NSXMLParser *myParser = [[NSXMLParser alloc] initWithData:xmlData]; 
[myParser setDelegate:self]; 
[myParser setShouldResolveExternalEntities: YES];
[myParser parse];
[myParser release];      

In the delegate method you will find attribute dictionary.

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

Here attributeDict will have all the attributes.

Apurv
  • 17,116
  • 8
  • 51
  • 67