XML不解析到NSDictionary?
问题描述:
我使用XMLReader转换的xmlString NSDictionary中,它工作正常的一些XML的string.But它给空字典XML低于XML不解析到NSDictionary?
<?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>
我转换的代码显示如下:
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];
URL被打印以下数据上的浏览器
0x00E0E0E00x00B4B4B40x0033001A0x005757570x003E3E370x0000CC330x00BBBBBB0x00A8A8A80x00C2C2C2
0x00796767
和印花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>
我认为这就是为什么它不转换为NSDictionary。 请帮忙如果你以前遇到过这样的问题。 当您将以上XML数据传递给[XMLReader dictionaryForXMLString:responseString error:nil]; 在响应字符串的地方,那么它会很好的工作。但是如果你执行上面的代码,它会显示dictionary =(null)。
在此先感谢!
答
终于找到了错误:
它计数转义序列响应string.That的,为什么不解析来的NSDictionary。 我已经添加了下面的行来用空白替换转义序列。
NSString *responseString=[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
responseString=[responseString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
现在我的代码运行完美。解析后输出如下所示。
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;
};
};
};
}
答
你尝试把分配
NSDictionary *dics=[XMLReader dictionaryForXMLData:urlData error:nil];
在异步请求的回调?它对我来说非常合适。
我在想,也许在第一个地方下载xml可能会有问题,但是你不检查。
如果你替换NSDictionary * dics = [XMLReader dictionaryForXMLData:urlData error:nil],你会在错误参数中得到什么?与NSError *错误=零; NSDictionary * dics = [XMLReader dictionaryForXMLData:urlData错误:&错误]; NSLog(@“error:%@”,[error localizedDescription]);你的错误应该在那里。 – Vlad
错误是(null) – Warewolf