2013-06-06 2 views
0

Мне нужно преобразовать словарь в формат XML для использования с SOAP webservice в объекте C.Преобразование NSDictionary в XML

Let the dictionary be { 
    password = testpassword; 
    username = testusername; 
} 
and the converted result i need is : 

<password>testpassword</password> 
<username>testusername</username> 

i need a specific function to get output like this format for dynamic dictioanry . 

many libraries are available but i dont get output like this . 

thanks in advance .. 
+1

Нужно ли это как файл? Строка? У вас есть схема? Два элемента не создают XML. – uchuugaka

ответ

3

Используйте, пожалуйста, нижеследующий код для преобразования NSDictionary в XML.

+(NSString*)ConvertDictionarytoXML:(NSDictionary*)dictionary withStartElement:(NSString*)startele 
{ 
    NSMutableString *xml = [[NSMutableString alloc] initWithString:@""]; 
    NSArray *arr = [dictionary allKeys]; 
    [xml appendString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"]; 
    [xml appendString:[NSString stringWithFormat:@"<%@>",startele]]; 
    for(int i=0;i<[arr count];i++) 
    { 
     id nodeValue = [dictionary objectForKey:[arr objectAtIndex:i]]; 
     if([nodeValue isKindOfClass:[NSArray class]]) 
     { 
      if([nodeValue count]>0){ 
       for(int j=0;j<[nodeValue count];j++) 
       { 
        id value = [nodeValue objectAtIndex:j]; 
        if([ value isKindOfClass:[NSDictionary class]]) 
        {  
         [xml appendString:[NSString stringWithFormat:@"<%@>",[arr objectAtIndex:i]]]; 
         [xml appendString:[NSString stringWithFormat:@"%@",[value objectForKey:@"text"]]]; 
         [xml appendString:[NSString stringWithFormat:@"</%@>",[arr objectAtIndex:i]]]; 
        } 

       } 
      } 
     } 
     else if([nodeValue isKindOfClass:[NSDictionary class]]) 
     { 
      [xml appendString:[NSString stringWithFormat:@"<%@>",[arr objectAtIndex:i]]]; 
      if([[nodeValue objectForKey:@"Id"] isKindOfClass:[NSString class]]) 
       [xml appendString:[NSString stringWithFormat:@"%@",[nodeValue objectForKey:@"Id"]]]; 
      else 
       [xml appendString:[NSString stringWithFormat:@"%@",[[nodeValue objectForKey:@"Id"] objectForKey:@"text"]]]; 
      [xml appendString:[NSString stringWithFormat:@"</%@>",[arr objectAtIndex:i]]]; 
     } 

     else 
     { 
      if([nodeValue length]>0){ 
       [xml appendString:[NSString stringWithFormat:@"<%@>",[arr objectAtIndex:i]]]; 
       [xml appendString:[NSString stringWithFormat:@"%@",[dictionary objectForKey:[arr objectAtIndex:i]]]]; 
       [xml appendString:[NSString stringWithFormat:@"</%@>",[arr objectAtIndex:i]]]; 
      } 
     } 
    } 

    [xml appendString:[NSString stringWithFormat:@"</%@>",startele]]; 
    NSString *finalxml=[xml stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"]; 
// NSLog(@"%@",xml); 
    return finalxml; 
} 
+0

удивительный. Я немного поправил его, и все получилось нормально. (см. ниже) – RemembranceNN

+1

Хм. Конечно, проблема связана с вложенными словарями. –

+0

Что такое параметр startele –

Смежные вопросы