OGeek|极客世界-中国程序员成长平台

标题: iphone - XML 到 NSdictionary 到 plist [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 08:05
标题: iphone - XML 到 NSdictionary 到 plist

大家好,我有一点奇怪的问题。我正在使用一个简单的 XML 到 NSDictionary 转换器 http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/ 但是如果打印 NSDictionary ,我可以看到节点的文本值,但如果我将它存储在 plist 中,文本节点将没有任何字符串值。见附图片。 i can see the text node value when i print NSdictionary

这是 plist 截图。

enter image description here

这里是代码

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);



Best Answer-推荐答案


我认为您的 plist 文件没有空值。您只是没有考虑第一张图像中突出显示文本中的换行符(“\n\thttp://wordpress.org/?v=3.0.1”)。

验证这一点的一种简单方法是使用文本编辑器打开它。你会发现值(value)观就在那里。您还可以在属性列表编辑器中导航到该字段并按下向下按钮。

替换换行符

使用 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];

关于iphone - XML 到 NSdictionary 到 plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6495167/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4