heightI是iphone开发的菜鸟,我试图用包含多个属性的元素解析xml。我有几个关于解析XML的教程,但它们都没有显示解析具有多个属性的元素的步骤。我试图用NSXMLParser解析xml。我想解析第一次出现的媒体:第二个属性的值的缩略图元素。非常感谢任何帮助。
我的代码:
// XML
<item>
<title>March 15 AM Metals Commentary: Eric Zuccarelli</title>
<link>http://link.brightcove.com/services/link/bcpid1683318714001/bclid1644543007001/bctid2228319176001?src=mrss</link>
<description>Eric Zuccarelli, Independent Copper Trader</description>
<guid>http://link.brightcove.com/services/link/bcpid1683318714001/bclid1644543007001/bctid2228319176001?src=mrss</guid>
<pubDate>Fri, 15 Mar 2013 06:40:38 -0700</pubDate>
<media:player height="546" url="http://link.brightcove.com/services/link/bcpid1683318714001/bclid1644543007001/bctid2228319176001?src=mrss" width="966" / >
<media:keywords>commentary,CME Group,financial products,cmedaily,nymex,metals,youtube,Market Commentary,Zuccarelli</media:keywords>
<media:thumbnail height="90" url="http://brightcove.vo.llnwd.net/d21/unsecured/media/49919183001/49919183001_2228333209001_th-514324d6e4b02e906f7476ba-806787304001.jpg?pubId=49919183001" width="120" / >
<media:thumbnail height="360" url="http://brightcove.vo.llnwd.net/d21/unsecured/media/49919183001/49919183001_2228333208001_vs-514324d6e4b02e906f7476ba-806787304001.jpg?pubId=49919183001" width="480" / >
<media:category>Metals</media:category>
<bc:playerid>1683318714001</bc:playerid>
<bc:lineupid>1644543007001</bc:lineupid>
<bc:titleid>2228319176001</bc:titleid>
<bc:duration>215</bc:duration>
<dcterms:valid / >
<bc:accountid>49919183001</bc:accountid>
</item>
我的代码如下:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentBright = [[NSMutableString alloc] init];
if([elementName isEqualToString:@"media:thumbnail"]){
if([attributeDict objectForKey:@"height"]==90){
currentBright = [attributeDict objectForKey:@"url"];
NSLog(@"Brightcove Url:%@", currentBright); //<--Not reaching this point.
}
}
}
}
修改
if([elementName isEqualToString:@"media:thumbnail"]&&[attributeDict objectForKey:@"height"]==90){
currentBright = [attributeDict objectForKey:@"
NSLog(@"Brightcove Url:%@", currentBright);
}
答案 0 :(得分:1)
试试这个......
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentBright = [[NSMutableString alloc] init];
}
else if([elementName isEqualToString:@"media:thumbnail"]) {
if([attributeDict objectForKey:@"height"]==90) {
currentBright = [attributeDict objectForKey:@"url"];
NSLog(@"Brightcove Url:%@", currentBright);
}
}
}
答案 1 :(得分:0)
你永远不会打你的第二个或第三个条件因为elementName如何在不升级的情况下改变了?相反,你应该取消你的条件。
现在发生的事情是:
-didStartTag
ElementName = item - &gt;第一次打击 - &gt;第二次失败
-didStartTag
ElementName = title - &gt;第一次失败
等...