2

我是 iphone 开发新手。我想从 xml 文件中解析图像 url 并将其显示在 RSS 提要中。有三个图像 url,但我只想检索一个 url 并显示它。

<entry>
<id>xxxxx</id>
<title>xxx xxxx xxxx</title>
<content>xxxxxxxxxxx</content>
<media:group>
<media:thumbnail url="http://tiger.jpg"/>
<media:thumbnail url="http://lion.jpg"/>
<media:thumbnail url="http://elephan.jpg"/>
</media:group>
</entry>
<entry>
<id>xxxxx</id>
<title>xxx xxxx xxxx</title>
<content>xxxxxxxxxxx</content>
<media:group>
<media:thumbnail url="http://parrot.jpg"/>
<media:thumbnail url="http://peacock.jpg"/>
<media:thumbnail url="http://sparrow.jpg"/>
</media:group>
</entry>

解析它

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName     namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes: (NSDictionary *)attributeDict{   

currentElement = [elementName copy];
if ([elementName isEqualToString:@"entry"]) {
entry = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
NSLog(@"inside image1 ");
}else if([elementName isEqualToString:@"media:thumbnail"])
{
if(myUrl==nil){
    NSString* myUrl = [NSString stringWithString:[attributeDict objectForKey:@"url"]];
}
}   

}.

我只想检索老虎和鹦鹉的图像。但是我得到了两次老虎。请帮帮我。谢谢。

4

2 回答 2

2

Simply keep a flag in your parser delegate that you reset when you see <media:group>. Then every time you see a <media:thumbnail> you get. Then every time you get a <media:thumbnail>, check if the flag has been set. If not then this is the first one, so you take the data and set the flag. When you see the next <media:thumbnail> you ignore it because the flag has been set.

In your case, myUrl is the flag. So simply reset it to nil every tie you see the <media:group>.

于 2010-02-15T15:09:25.253 回答
1

当您开始将entry元素重置myUrl变量解析为 nil - 因此您将为每个“条目”元素解析一次“媒体:缩略图”

于 2010-02-15T15:09:15.393 回答