Iphone 将NSDictionary转换为XML
我需要将字典转换为XML格式,以便在objective C中与SOAP webservice一起使用Iphone 将NSDictionary转换为XML,iphone,objective-c,xml,nsdictionary,Iphone,Objective C,Xml,Nsdictionary,我需要将字典转换为XML格式,以便在objective C中与SOAP webservice一起使用 Let the dictionary be { password = testpassword; username = testusername; } and the converted result i need is : <password>testpassword</password> <username>testusername&
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 ..
把字典放在一边{
密码=测试密码;
用户名=testusername;
}
我需要的转换结果是:
测试密码
testusername
我需要一个特定的函数来获得动态字典的输出,就像这种格式。
很多库都可用,但我没有得到这样的输出。
提前谢谢。。
请使用以下代码将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:@"&"];
// NSLog(@"%@",xml);
return finalxml;
}
+(NSString*)ConvertDictionarytoXML:(NSDictionary*)带起始元素的字典:(NSString*)startele
{
NSMutableString*xml=[[NSMutableString alloc]initWithString:@'';
NSArray*arr=[字典所有键];
[xml appendString:@”“;
[xml appendString:[NSString stringWithFormat:@',startele]];
对于(int i=0;i0){
对于(int j=0;j0){
[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:@”和“with string:@”和“];
//NSLog(@“%@”,xml);
返回finalxml;
}
请使用以下代码将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:@"&"];
// NSLog(@"%@",xml);
return finalxml;
}
+(NSString*)ConvertDictionarytoXML:(NSDictionary*)带起始元素的字典:(NSString*)startele
{
NSMutableString*xml=[[NSMutableString alloc]initWithString:@'';
NSArray*arr=[字典所有键];
[xml appendString:@”“;
[xml appendString:[NSString stringWithFormat:@',startele]];
对于(int i=0;i0){
对于(int j=0;j0){
[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:@”和“with string:@”和“];
//NSLog(@“%@”,xml);
返回finalxml;
}
试试这个第三方软件
. 它应该能用试试这个第三方软件
. 它应该能用你需要它作为一个文件吗?一根绳子?你有一个模式吗?两个元素不能构成XML。您需要它作为文件吗?一根绳子?你有一个模式吗?两个元素不能使XML.awesome变好。我把它改了一点,效果很好。(见下文)嗯。处理嵌套字典肯定有问题。什么是startele参数。我把它改了一点,效果很好。(见下文)嗯。处理嵌套字典肯定有问题。startele参数是什么