菜鸟教程小白 发表于 2022-12-13 08:05:34

iphone - XML 到 NSdictionary 到 plist


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

<p>这是 plist 截图。</p>

<p> <img src="/image/Qj1wg.png" alt="enter image description here"/> </p>

<p>这里是代码</p>

<pre><code>NSURL * URL = [initWithString:@&#34;rss feed link &#34;];
    NSData * data = [ initWithContentsOfURL:URL];
    NSString *testXMLString = [initWithData:data encoding:NSUTF8StringEncoding];

    // Parse the XML into a dictionary
    NSError *parseError = nil;
    NSDictionary *xmlDictionary = ;
    NSString * filepath = ];
;


    // Print the dictionary
    NSLog(@&#34;%@&#34;, xmlDictionary);
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为您的 <code>plist</code> 文件没有空值。您只是没有考虑第一张图像中突出显示文本中的换行符(<code>“\n\thttp://wordpress.org/?v=3.0.1”</code>)。</p >

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

<p><strong>替换换行符</strong></p>

<p>使用 <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByReplacingOccurrencesOfString%3awithString%3a" rel="noreferrer noopener nofollow"><code>stringByReplacingOccurrencesOfString:withString:</code></a>用 <code>@""</code> 替换所有换行符的方法。</p>

<pre><code>testXMLString = ;
</code></pre>

<p><strong>更好的修剪</strong></p>

<p>因此,替换制表符 (<code>\t</code>) 似乎会使 <code>XMLReader</code> 无法读取字符串。所以要解决这个问题,更改 <code>XMLReader</code> 代码以将修剪后的值插入字典会更容易。</p>

<p>在方法<code>parser:didEndElement:namespaceURI:qualifiedName:</code>中,替换</p>

<pre><code>;
</code></pre>

<p>与</p>

<pre><code>NSString * trimmedString = ];
;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - XML 到 NSdictionary 到 plist,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/6495167/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/6495167/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - XML 到 NSdictionary 到 plist