0

This is my xml code:

<root><article>
   <title>Test media</title>
   <media>
     <thumbnail>thumbnail1.png</thumbnail>
     <full>full1.png</full>
     <title>thumb1</title>
  </media>
  <media>
     <thumbnail>thumbnail2.png</thumbnail>
     <full>full2.png</full>
     <title>thumb2</title>
  </media>
  <media>
     <thumbnail>thumbnail3.png</thumbnail>
     <full>full3.png</full>
     <title>thumb3</title>
  </media></article></root>

How I can get all info (thumbnail,full,title) of the media nodes and title. Thanks !

4

1 回答 1

0

您可以使用 NSXMLParser 类的以下方法。当你得到标签的开始和结束时,你可以按照你需要的方式放置你的逻辑。

/---when the start of an element is found---
-(void)  parser:(NSXMLParser *) parser 
 didStartElement:(NSString *) elementName 
 namespaceURI:(NSString *) namespaceURI 
 qualifiedName:(NSString *) qName
 attributes:(NSDictionary *) attributeDict
{
    if( [elementName isEqualToString:@"media"])
    {
        if (!soapResultsPortFolio)
        {
            soapResultsPortFolio = [[NSMutableString alloc] init];
        }
        elementFoundPortFolio = TRUE;
        NSLog(@"Registration...%@",soapResultsPortFolio);
    }
    else if([elementName isEqualToString:@"thumbnail"])
    {
        elementFoundPortFolio = TRUE;
    }
    else if([elementName isEqualToString:@"full"])
    {
        elementFoundPortFolio = TRUE;
    }
    else if([elementName isEqualToString:@"title"])
    {
        elementFoundPortFolio = TRUE;
    }

}


 //---when the end of element is found---
-(void)parser:(NSXMLParser *)parser 
didEndElement:(NSString *)elementName 
 namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"media"])
    {          
        NSLog(@"display the soap results%@",soapResultsPortFolio);
    }
    else if([elementName isEqualToString:@"thumbnail"])
    {          
        //Perform required action
    }
    else if([elementName isEqualToString:@"full"])
    {
        //Perform required action
    }
    else if([elementName isEqualToString:@"title"])
    {
        //Perform required action
    }

    [soapResultsPortFolio setString:@""];
    elementFoundPortFolio = FALSE;
}
于 2013-01-16T07:45:52.667 回答