[英]XML not parsing to NSDictionary?
I am using XMLReader for converting xmlString to NSDictionary and it is working fine to some of the xml string.But it is giving null dictionary for xml displayed below 我正在使用XMLReader将xmlString转换为NSDictionary,它对一些xml字符串工作正常。但它为xml提供了空字典,显示如下
<?xml version=\"1.0\"?>
<THEME>
<OBJ>
<FORMBG>0x00E0E0E0</FORMBG>
<ALERTBG>0x00B4B4B4</ALERTBG>
<FORMFONT>0x0033001A</FORMFONT>
<FORMFONT2>0x00575757</FORMFONT2>
<FORMFONT3>0x003E3E37</FORMFONT3>
<ROWSELECTORLIST>0x0000CC33</ROWSELECTORLIST>
<ROWDIVIDER>0x00BBBBBB</ROWDIVIDER>
<SUBHEADER>0x00A8A8A8</SUBHEADER>
<SELECTORCOMPONENT>0x00C2C2C2</SELECTORCOMPONENT>
<FOOTER>0x00796767</FOOTER>
</OBJ>
</THEME>
my conversion code is as follows: 我的转换代码如下:
NSString *stringURL=[NSString stringWithFormat:@"http://202.58.232.138/mt/theme.aspx"];
NSURL *url=[NSURL URLWithString:stringURL];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:url];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString=[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"response string is %@",responseString);
NSDictionary *dictionary=[XMLReader dictionaryForXMLString:responseString error:nil];
The url is printing following data on browser 网址正在浏览器上打印数据
0x00E0E0E00x00B4B4B40x0033001A0x005757570x003E3E370x0000CC330x00BBBBBB0x00A8A8A80x00C2C2C2
0x00796767
and printing on xcode log will display following data. 并在xcode日志上打印将显示以下数据。
<?xml version=\"1.0\"?>
<THEME>
<OBJ>
<FORMBG>0x00E0E0E0</FORMBG>
<ALERTBG>0x00B4B4B4</ALERTBG>
<FORMFONT>0x0033001A</FORMFONT>
<FORMFONT2>0x00575757</FORMFONT2>
<FORMFONT3>0x003E3E37</FORMFONT3>
<ROWSELECTORLIST>0x0000CC33</ROWSELECTORLIST>
<ROWDIVIDER>0x00BBBBBB</ROWDIVIDER>
<SUBHEADER>0x00A8A8A8</SUBHEADER>
<SELECTORCOMPONENT>0x00C2C2C2</SELECTORCOMPONENT>
<FOOTER>0x00796767</FOOTER>
</OBJ>
</THEME>
I think that's why it is not converting to NSDictionary. 我认为这就是它没有转换为NSDictionary的原因。 please help If you have faced such a problem before. 请帮忙如果你以前遇到过这样的问题。 When you pass above xml data to for [XMLReader dictionaryForXMLString:responseString error:nil]; 当你将上面的xml数据传递给[XMLReader dictionaryForXMLString:responseString error:nil]; at the place of response string then it will work perfectly.But if you execute above code it will display dictionary =(null). 在响应字符串的位置然后它将完美地工作。但是如果你执行上面的代码它将显示dictionary =(null)。
Thanks in advance! 提前致谢!
Finally found the mistake: 终于找到了错误:
it is counting escape sequence in response string.That's why not parsing to NSDictionary. 它在响应字符串中计算转义序列。这就是为什么不解析为NSDictionary。 I have added below line to replace escape sequence with blank. 我在下面添加了一行,用空格替换转义序列。
NSString *responseString=[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
responseString=[responseString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
Now my code is running perfectly. 现在我的代码运行得很好。 output is showing below after parsing. 解析后输出如下所示。
xml dictianary is {
THEME = {
OBJ = {
ALERTBG = {
text = 0x00B4B4B4;
};
FOOTER = {
text = 0x00796767;
};
FORMBG = {
text = 0x00E0E0E0;
};
FORMFONT = {
text = 0x0033001A;
};
FORMFONT2 = {
text = 0x00575757;
};
FORMFONT3 = {
text = 0x003E3E37;
};
ROWDIVIDER = {
text = 0x00BBBBBB;
};
ROWSELECTORLIST = {
text = 0x0000CC33;
};
SELECTORCOMPONENT = {
text = 0x00C2C2C2;
};
SUBHEADER = {
text = 0x00A8A8A8;
};
};
};
}
I guess dictionary is not initialized. 我猜词典没有初始化。
You should initialize NSDictionary *dics. 你应该初始化NSDictionary * dics。
Did you try putting the allocation 你尝试过分配了吗?
NSDictionary *dics=[XMLReader dictionaryForXMLData:urlData error:nil];
in the callback of an asynchronous request ? 在异步请求的回调中? It works perfectly for me. 它对我来说很完美。
I'm thinking maybe there can be a problem downloading the xml in the first place but you don't check for that. 我想也许首先下载xml可能有问题,但你没有检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.