如何限制iPhone中的解析?
我是 iphone 开发新手。我想从 xml 文件解析图像 url 并将其显示在 RSS 提要中。有 3 个图像 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"]];
}
}
}.
我只想检索老虎和鹦鹉图像。但是我得到了老虎两次。请帮助我。谢谢。
I am new to iphone development .I want parse an image url from a xml file and display it in a RSS feed.There are three image url but i want to retrieve only one url and display it.
<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>
for parsing it
- (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"]];
}
}
}.
I want to retrieve only tiger and parrot image.But i get tiger twice.Please help me out.Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您开始解析
entry
元素时,将myUrl
变量重置为 nil - 因此您将为每个“entry”元素解析一次“media:thumbnail”When you start parsing your
entry
element resetmyUrl
variable to nil - so you will parse "media:thumbnail" once for each 'entry' element只需在解析器委托中保留一个标志,当您看到
时即可重置该标志。然后每次您看到
时都会得到。然后每次收到
时,检查该标志是否已设置。如果不是,那么这是第一个,因此您获取数据并设置标志。当您看到下一个
时,您会忽略它,因为该标志已设置。在您的情况下,
myUrl
是标志。因此,只需将您看到
的每一条关系重置为 nil 即可。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>
.