简体   繁体   English

来自xml的NSdictionary元素

[英]NSdictionary element from xml

i have a situation I am getting data from the server as xml, i converted that data into NSDictionary using https://github.com/Insert-Witty-Name 我有一种情况我从服务器获取数据为xml,我使用https://github.com/Insert-Witty-Name将该数据转换为NSDictionary

MY xml is like 我的xml就像

<Game>
      <userid></userid>
      <name></name>
      <friendId></friendId>
      <friendName></friendName>
</Game>

the value Game and all other items can change, so first I need to find out the name of the first element that is a game => 值Game和所有其他项可以改变,所以首先我需要找出第一个元素的名称是游戏=>

I converted this through above Library now I am getting as 我现在通过上面的图书馆转换了这个

 xmlString = [NSString stringWithFormat:@"<Game><userid>12</userid><userName>ben</userName><friendId>41</friendId><friendName>Ben1</friendName></Game>"];

NSDictionary *dict = [XMLReader dictionaryForXMLString:xmlString error:nil];

NSString *string;
string = [dict objectForKey:@"Game"];
NSLog(@"name is :%@",string);

and the output is 而输出是

{
friendId = 41;
friendName = Ben1;
userName = ben;
userid = 12;
}

I need to find out the root element name of xml and how to access these values in NSDictionary with objectForKey 我需要找出xml的根元素名称以及如何使用objectForKey在NSDictionary中访问这些值

string = [[dict objectForKey:@"Game"] objectForKey:@"userName"]; 

use Enumerator to iterate and find value based on the key or 使用Enumerator迭代并根据键或值查找值
[[dict objectForKey:@"Game"] allKeys] to get all keys [[dict objectForKey:@"Game"] allKeys]获取所有密钥

So you want the result to be "ben" , correct? 所以你希望结果是"ben" ,对吗? Well, [dict objectForKey:@"Game"]; 好吧, [dict objectForKey:@"Game"]; is not an NSString but an NSDictionary . 不是NSString而是NSDictionary You get no error because there is no way for LLVM to check this at compile time, so it happily will build. 你没有得到任何错误,因为LLVM无法在编译时检查这个错误,因此它很乐意构建。 The conversion to a string happens in the parsing into the format string "%@" . 转换为字符串时会在解析为格式字符串"%@" Instead, what you want is this: 相反,你想要的是这个:

string = [[dict objectForKey:@"Game"] objectForKey:@"name"];

EDIT: Different question as becomes clear from the comments... 编辑:从评论中可以清楚地看到不同的问题......

Or do you rather need "Game" ? 或者你更喜欢"Game"吗? In which case 在这种情况下

NSString* rootName = [[dict allKeys] firstObject];

Will give you always the correct answer, since there always is only one root. 会给你总是正确的答案,因为总有一个根。

If your xml can change 如果你的xml可以改变

this: 这个:

<Game>
      <userid></userid>
      <name></name>
      <friendId></friendId>
      <friendName></friendName>
</Game>

becoming this: 变成这样:

<Status>
      <userid></userid>
      <name></name>
      <friendId></friendId>
      <friendName></friendName>
</Status>

then you have to dynamically find the dictionary keys, using the method allKeys 然后你必须使用allKeys方法动态查找字典键

like this: 像这样:

[dict allKeys]

on my first example the result will be: ["Game"] and on the second: ["Status"] 在我的第一个例子中,结果将是: ["Game"]和第二个: ["Status"]

if you need the keys of the inner element just do like this: 如果你需要内部元素的键,就这样做:

[[dict objectForKey:@"Game"] allKeys]

this will return: ["userid","name","friendId", "friendName"] 这将返回: ["userid","name","friendId", "friendName"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM