views:

202

answers:

3

I have gone through following question.

http://stackoverflow.com/questions/998554/convert-nsstring-to-nsdictionary

It is something different then my question.

My question is as follows.

NSString *x=@"<Category_Id>5</Category_Id><Category_Name>Motos</Category_Name><Category_Picture>http://192.168.32.20/idealer/admin/Picture/icon_bike2009819541578.png&lt;/Category_Picture&gt;";

Now I want to convert this into a dictionary, something like this,

dictionary key = Category_Id      | value = 5 
dictionary key = Category_Name    | value = ???
dictionary key = Category_Picture | value = ???

Don't know how to perform this?

Thanks for sharing your knowledge.

Sagar

+4  A: 

If it's XML then you can use an NSXMLParser. If it's not then you can write your own parser.

Azeem.Butt
Your format is not XML. You have a pipe-delimited string in your post that you want to convert. You'll need to write your own parser.
Marc W
And you'd prefer to represent XML text with a data type that isn't a string? What?
Azeem.Butt
sugar
The pipe-delimited stuff in the OP is just pretty-printing the desired dictionary contents.
Wevah
+2  A: 

You could do it with a regular expression... Something like <([^>]+)>([^<]+)</\1> would grab the key into capture 1 and the value into capture 2. Iterate over the matches and build the dictionary.

This uses RegexKitLite:

NSString  * x = @"<Category_Id>5</Category_Id><Category_Name>Motos</Category_Name><Category_Picture>http://192.168.32.20/idealer/admin/Picture/icon_bike2009819541578.png&lt;/Category_Picture&gt;";
NSString * regex = @"<([^>]+)>([^<]+)</\\1>";
NSArray * cap = [x arrayOfCaptureComponentsMatchedByRegex:regex];
NSMutableDictionary * d = [NSMutableDictionary dictionary];
for (NSArray * captures in cap) {
 if ([captures count] < 3) { continue; }
 NSString * key = [captures objectAtIndex:1];
 NSString * value = [captures objectAtIndex:2];
 [d setObject:value forKey:key];
}
NSLog(@"%@", d);
Dave DeLong
Sir, Would you give some more brief, please?
sugar
Sagar, check out the links I posted in your question on string matching. In general learning about regular expressions will be very helpful for you: http://stackoverflow.com/questions/1633331/replacing-in-nsstring-with-wildcards-regular-expressions-cocoa
nall
Glad to know sir. Very smart.
sugar
@sagar - edited answer.
Dave DeLong
sugar
a long time? 2 is hours a long time?
Dave DeLong
sugar
@Dave Sir, I think @Chris have better option. We don't require get into regex. What's your opinion sir?
sugar
not being familiar with `NSXMLDocument`, I have no idea. Looks like a perfectly legit answer to me. What option you use is entirely up to you.
Dave DeLong
+1  A: 

Not the fastest implementation, but this would do the trick (and doesn’t require any third party libraries):

@interface NSDictionary (DictionaryFromXML)

+ (NSDictionary *)dictionaryFromXML:(NSString *)xml;

@end

@implementation NSDictionary (DictionaryFromXML)

+ (NSDictionary *)dictionaryFromXML:(NSString *)xml
{
  // We need to wrap the input in a root element
  NSString *x = [NSString stringWithFormat:@"<x>%@</x>", xml];

  NSXMLDocument *doc = [[[NSXMLDocument alloc] initWithXMLString:x
                                                         options:0
                                                           error:NULL]
                         autorelease];

  if (!doc)
    return nil;

  NSMutableDictionary *dict = [NSMutableDictionary dictionary];

  for (NSXMLElement *el in [[doc rootElement] children])
    [dict setObject:[el stringValue] forKey:[el name]];

  return dict;
}

@end
Chris Suter
Yeh ! this is what I need.
sugar
Must Be voted up +10 or higher. ( according to me ).
sugar