大家好我有一点奇怪的问题。我使用简单的XML到NSDictionary转换器
http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/
但是如果打印NSDictionary,我可以看到节点的文本值,但如果我将它存储在plist中,文本节点将没有任何字符串值。请参阅附加图片。
这是plist截图。
这是代码
NSURL * URL = [[NSURL alloc]initWithString:@"rss feed link "];
NSData * data = [[NSData alloc] initWithContentsOfURL:URL];
NSString *testXMLString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
// Parse the XML into a dictionary
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
NSString * filepath = [self dataFilePathwithFilename:[NSString stringWithFormat:@"TEST.plist"]];
[xmlDictionary writeToFile:filepath atomically:YES];
// Print the dictionary
NSLog(@"%@", xmlDictionary);
答案 0 :(得分:3)
我认为您的plist
文件没有空值。您只是不考虑第一张图像("\n\thttp://wordpress.org/?v=3.0.1"
)中高亮文本中的换行符。
验证这一点的一种简单方法是使用文本编辑器打开它。你会发现值存在。您还可以在属性列表编辑器中导航到该字段,然后按向下按钮。
替换换行符
使用stringByReplacingOccurrencesOfString:withString:
方法将所有换行符替换为@""
。
testXMLString = [testXMLString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
更好的修剪
因此,替换标签(\t
)似乎使XMLReader
无法读取该字符串。因此,为了解决这个问题,更改XMLReader
代码以将修剪后的值插入字典会更容易。
在方法parser:didEndElement:namespaceURI:qualifiedName:
中,替换
[dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey];
与
NSString * trimmedString = [textInProgress stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n\t"]];
[dictInProgress setObject:trimmedString forKey:kXMLReaderTextNodeKey];