0

高度我是 iphone 开发的菜鸟,我正在尝试使用包含多个属性的元素解析 xml。我有几个关于解析 XML 的教程,但没有一个显示解析具有多个属性的元素的步骤。我正在尝试使用 NSXMLParser 解析 xml。我想解析 media:thumbnail 元素的第一次出现以获取第二个属性的值。任何帮助是极大的赞赏。

我的代码:

//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);
}
4

2 回答 2

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); 
    }
 }
}
于 2013-03-15T20:43:54.137 回答
0

你永远不会达到你的第二个或第三个条件,因为 elementName 怎么能在没有被推进的情况下发生变化?相反,您应该取消嵌套条件。

现在发生的事情是:

-didStartTag

ElementName = item -> 第一个条件命中 -> 第二个失败

-didStartTag

ElementName = title -> 第一个条件失败

ETC....

于 2013-03-15T20:11:06.350 回答