0

我正在尝试解析这个 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" version="2.0">
    <channel>
        <title><![CDATA[App Reviews for Free - Reviews]]></title>
        <link><![CDATA[http://appreviewsforfree.weebly.com/reviews.html]]></link>
        <description><![CDATA[Reviews]]></description>
        <pubDate>Fri, 18 Oct 2013 15:06:45 -0600</pubDate>
        <generator>Weebly</generator>
        <item>
            <title><![CDATA[Asphalt 8 Review]]></title>
            <link><![CDATA[http://appreviewsforfree.weebly.com/1/post/2013/10/asphalt-8-review.html]]></link>
            <comments><![CDATA[http://appreviewsforfree.weebly.com/1/post/2013/10/asphalt-8-review.html#comments]]></comments>
            <pubDate>Fri, 18 Oct 2013 21:30:46 GMT</pubDate>
            <category><![CDATA[Uncategorized]]></category>
            <guid isPermaLink="false">http://appreviewsforfree.weebly.com/1/post/2013/10/asphalt-8-review.html</guid>
            <description><![CDATA[I HATE racing games and have really bad experience with them. I was really hesitant to download Asphalt 8 especially because it was paid. However right now it is the most used app on my iPad! Asphalt 8 has graphics so good it really looks like a game from the future. I really recommend it and hope you enjoy it as much as I do.Positives:Graphics are truly beautifulControls are easy to learn [...]]]></description>
            <content:encoded>&lt;div class="paragraph" style="text-align:left;"&gt;I &lt;strong&gt;HATE &lt;/strong&gt;racing games and have really bad experience with them. I was really hesitant to download Asphalt 8 especially because it was paid. However right now it is the most used app on my iPad! Asphalt 8 has graphics so good it really looks like a game from the future. I really recommend it and hope you enjoy it as much as I do.&lt;br /&gt;&lt;br /&gt;Positives:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Graphics are truly beautiful&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Controls are easy to learn&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Level Difficulty is just right&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;Negatives:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Sometimes gets frustrating&amp;nbsp;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;  &lt;div class="wsite-youtube" style="margin-bottom:10px;marg<![CDATA[in-top:10px;"><div class="wsite-youtube-wrapper wsite-youtube-size-hd wsite-youtube-align-center">     <div class="wsite-youtube-container">                       <iframe src="http://www.youtube.com/embed/eoUEPu68Vww?wmode=opaque" frameborder="0" allowfullscreen></iframe>   </div> </div></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Burt Destruction 2 Review]]></title>
            <link><![CDATA[http://appreviewsforfree.weebly.com/1/post/2013/10/burt-destruction-2-review.html]]></link>
            <comments><![CDATA[http://appreviewsforfree.weebly.com/1/post/2013/10/burt-destruction-2-review.html#comments]]></comments>
            <pubDate>Fri, 18 Oct 2013 14:52:23 GMT</pubDate>
            <category><![CDATA[Uncategorized]]></category>
            <guid isPermaLink="false">http://appreviewsforfree.weebly.com/1/post/2013/10/burt-destruction-2-review.html</guid>
            <description><![CDATA[Always wished there was Mario in the App Store? Introducing the next Mario: Bart Destruction 2!&nbsp;&nbsp;Bart Destruction 2 is an AMAZING game in which you run to the end. The controls are so easy: Just tap to jump and double tap to jump really high. You have to jump on top of enemies to kill them. I really recommend this game to all gamers.Positives:Really easy controls&nbsp;Gr [...]]]></description>
            <content:encoded>&lt;div class="paragraph" style="text-align:left;"&gt;Always wished there was Mario in the App Store? Introducing the next Mario: Bart Destruction 2!&amp;nbsp;&amp;nbsp;Bart Destruction 2 is an &lt;strong&gt;AMAZING &lt;/strong&gt;game in which you run to the end. The controls are so easy: Just tap to jump and double tap to jump really high. You have to jump on top of enemies to kill them. I really recommend this game to all gamers.&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Positives:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Really easy controls&amp;nbsp;&lt;br&gt;&lt;/li&gt;&lt;li&gt;Great Graphics&lt;br&gt;&lt;/li&gt;&lt;li&gt;You can record your game&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Negatives:&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Needs Mini-games&lt;br&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;/div&gt;  &lt;div class="wsite-youtube" style="margin-bottom:10px;margin-top:10px;"&gt;&lt;div class="wsite-yo<![CDATA[utube-wrapper wsite-youtube-size-auto wsite-youtube-align-center">    <div class="wsite-youtube-container">                       <iframe src="http://www.youtube.com/embed/pQi_-ZBGEWs?wmode=opaque" frameborder="0" allowfullscreen></iframe>   </div> </div></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Movie Clock HD Review]]></title>
            <link><![CDATA[http://appreviewsforfree.weebly.com/1/post/2013/10/movie-clock-hd-review.html]]></link>
            <comments><![CDATA[http://appreviewsforfree.weebly.com/1/post/2013/10/movie-clock-hd-review.html#comments]]></comments>
            <pubDate>Thu, 17 Oct 2013 14:05:09 GMT</pubDate>
            <category><![CDATA[Uncategorized]]></category>
            <guid isPermaLink="false">http://appreviewsforfree.weebly.com/1/post/2013/10/movie-clock-hd-review.html</guid>
            <description><![CDATA[Hate waiting for movies to come out? Want to see what others are saying about a movie? We found a perfect solution! &nbsp;Introducing the number one move app: Movie Clock HD.&nbsp;Movie Clock HD is an app in which you can see when a movie is coming out, see what others are saying about, and follow a specific movie.&nbsp;Positives:Amazing UIEasy to useAccurate&nbsp;N [...]]]></description>
            <content:encoded>&lt;div class="paragraph" style="text-align:left;"&gt;Hate waiting for movies to come out? Want to see what others are saying about a movie? We found a perfect solution! &amp;nbsp;Introducing the number one move app: Movie Clock HD.&amp;nbsp;Movie Clock HD is an app in which you can see when a movie is coming out, see what others are saying about, and follow a specific movie.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Positives:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Amazing UI&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Easy to use&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Accurate&amp;nbsp;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;Negatives&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Still can't find one!&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;  &lt;div class="wsite-youtube" style="margin-bottom:10px;margin-top:10px;"&gt;&lt;div class="wsite-youtube-wrapper wsite-youtube-size-auto wsite-youtube-align-center"&gt;   &lt;div class="wsite-youtube-container"&gt;                         &lt;iframe src="http://www.youtube.com/embed/Kc19Q3HNVmc?wmode=opaque" f<![CDATA[rameborder="0" allowfullscreen></iframe>   </div> </div></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Apache 3D Sim Review]]></title>
            <link><![CDATA[http://appreviewsforfree.weebly.com/1/post/2013/10/apache-3d-sim-review.html]]></link>
            <comments><![CDATA[http://appreviewsforfree.weebly.com/1/post/2013/10/apache-3d-sim-review.html#comments]]></comments>
            <pubDate>Thu, 17 Oct 2013 12:15:49 GMT</pubDate>
            <category><![CDATA[Uncategorized]]></category>
            <guid isPermaLink="false">http://appreviewsforfree.weebly.com/1/post/2013/10/apache-3d-sim-review.html</guid>
            <description><![CDATA[Did you always want to fly a helicopter? Did you ever want to go on hard and fun missions? We found the game for you! Apache 3D Sim is a game in which you can fly a helicopter, go on missions, or just fly around for fun.Positives:Easy to controlGreat GraphicsNegatives:Needs more missions   [...]]]></description>
            <content:encoded><![CDATA[<div class="paragraph" style="text-align:left;">Did you always want to fly a helicopter? Did you ever want to go on hard and fun missions? We found the game for you! Apache 3D Sim is a game in which you can fly a helicopter, go on missions, or just fly around for fun.<br /><br />Positives:<br /><ul><li>Easy to control<br /></li><li>Great Graphics<br /></li></ul><br /><br />Negatives:<br /><ul><li>Needs more missions</li></ul></div>  <div class="wsite-youtube" style="margin-bottom:10px;margin-top:10px;"><div class="wsite-youtube-wrapper wsite-youtube-size-auto wsite-youtube-align-center">    <div class="wsite-youtube-container">                       <iframe src="http://www.youtube.com/embed/K6zTiBOMkXg?wmode=opaque" frameborder="0" allowfullscreen></iframe>   </div> </div></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Infinity Blade 3 Review]]></title>
            <link><![CDATA[http://appreviewsforfree.weebly.com/1/post/2013/10/infinity-blade-3-review.html]]></link>
            <comments><![CDATA[http://appreviewsforfree.weebly.com/1/post/2013/10/infinity-blade-3-review.html#comments]]></comments>
            <pubDate>Wed, 16 Oct 2013 20:24:07 GMT</pubDate>
            <category><![CDATA[Uncategorized]]></category>
            <guid isPermaLink="false">http://appreviewsforfree.weebly.com/1/post/2013/10/infinity-blade-3-review.html</guid>
            <description><![CDATA[Infinity Blade 3 is a fantastic game with a lot of ups and downs. It is the last game of the Infinity Blade trilogy and is really worth the $7. Some users are complaining about crashes but it never crashed on me. I guess it only crashes on older devices.Positives:Amazing graphicsGreat StoryGet to play 2 charactersStays fun even when you finish the game [...]]]></description>
            <content:encoded><![CDATA[<div class="paragraph" style="text-align:left;">Infinity Blade 3 is a <strong>fantastic </strong>game with a lot of ups and downs. It is the last game of the Infinity Blade trilogy and is really worth the $7. Some users are complaining about crashes but it never crashed on me. I guess it only crashes on older devices.<br /><br />Positives:<br /><ul><li>Amazing graphics<br /></li><li>Great Story<br /></li><li>Get to play 2 characters<br /></li><li>Stays fun even when you finish the game<br /></li></ul><br /><br />Negatives:<br /><ul><li>The price<br /></li><li>Game is short and a little easy<br /></li><li>Takes so long loading<br /></li><li>Crashes on older devices<br /></li></ul><br />Short video of Infinity Blade 3 Gameplay:</div>  <div class="wsite-youtube" style="margin-bottom:10px;margin-top:10px;"><div class="wsite-youtube-wrapper wsite-youtube-size-auto wsite-youtube-align-center">  <div class="wsite-youtube-container">                       <iframe src="http://www.youtube.com/embed/uxDzaFg34ms?wmode=opaque" frameborder="0" allowfullscreen></iframe>   </div> </div></div>]]></content:encoded>
        </item>
    </channel>
</rss>

使用此代码:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"item"]) {
        isBlogPost = YES;
    }
    if ([elementName isEqualToString:@"title"] && isBlogPost == YES) {
        NSString *reviewName = [NSString stringWithFormat:@"%@",[attributeDict objectForKey:elementName]];
        NSLog(@"%@",reviewName);
    }
}

但是 NSLogs 返回 nil。为什么会这样?对不起,如果这是一个愚蠢的问题,但我从未解析过 xml。

4

1 回答 1

1

attributeDict返回开始标签内的任何内容。例如,您的 XML 示例包含一个如下所示的标记:

<guid isPermaLink="false">

在这种情况下,attributeDict将有一个键,isPermaLink,其值为false

但是该<title>元素没有任何属性,所以这就是它attributesDict为空的原因。

您想使用该foundCharacters方法返回在<title>和之间找到的字符</title>

有关详细信息,请参阅事件驱动 XML 编程指南的处理 XML 元素和属性部分。

于 2013-10-20T17:01:47.007 回答