如何限制iphone中的xml解析
我是 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>
为了解析它,
- (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"])
{
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>
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"])
{
NSString* myUrl = [NSString stringWithString:[attributeDict objectForKey:@"url"]];
}
}.
I want to retrieve only tiger image.Please help me out.Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这个特定的实例中,您可以设置一个类变量而不是当前的本地 myUrl,并且一旦您在其中获取了值。在下面的代码中,假设您从未在其他任何地方初始化 myURL。
但是,如果您始终想要第一个缩略图 URL,则这只会按您期望的方式工作。除非您有某种保证(您没有提到)您将始终需要第一个 URL,否则您实际上无法采取任何措施来捕获不需要第一个 URL 的情况,因为所有缩略图 URL 标记除了 URL 之外,属性也相同。
In this perticular instance you could set a class variable instead of the current local myUrl and once you get a value in it. In the code below this assumes you never initialize myURL anywhere else.
However this will only work the way you expect if you always want the first thumbnail URL. Unless you have some guarantee (which you haven't mentioned) that you will always want the first URL then there really isn't anything you can do to catch cases when you don't want the first URL since all of the thumbnail URLs tags and attributes are the same save for the URL.