【问题标题】:Having trouble understanding NSXMLParser无法理解 NSXMLParser
【发布时间】:2013-02-15 12:58:15
【问题描述】:

我无法理解 NSXMLParser。我尝试了一些外部 XML 解析器,但没有运气。我要做的是解析 GDATA Youtube API xml,它检索视频的 xml。这工作正常,我有 xml,但很难重复元素。我只需要一个可以检索元素的示例,例如 (media:thumbnail url="" )(/media:thumbnail)

如何从元素 media:thumbnail 中获取 url 值

我请求的网址是https://gdata.youtube.com/feeds/api/videos?q=football+-soccer&orderby=published&start-index=11&max-results=10&v=2

【问题讨论】:

    标签: iphone objective-c xml xcode


    【解决方案1】:

    这就是你可以使用NSXMLParser的方法:

    在您的 .h 文件中声明:

    NSMutableData       *webPortFolio;
    NSMutableString     *soapResultsPortFolio;
    NSURLConnection     *conn;
    
    //---xml parsing---
    
    NSXMLParser         *xmlParserPortFolio;
    BOOL                elementFoundPortFolio;
    NSMutableURLRequest *req;
    
    NSString            *theXMLPortFolio;
    NSString            *strSoapMsg;
    UIAlertView         *alertView;
    

    在您的 .m 文件中使用以下代码:

    -(void)callURL
    {
    
         //Your logic to call URL.
    
         conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
         if (conn)
         {
             webPortFolio = [[NSMutableData data] retain];
         }
    }
    

    为了处理响应,您可以使用以下函数:

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        [webPortFolio setLength:0];     
    }
    
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [webPortFolio appendData:data];
    }
    
    -(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error
    {
    
        NSLog(@"error...................%@",[error description]);
        [webPortFolio release];
        [connection release];
    }
    
    -(void) connectionDidFinishLoading:(NSURLConnection *) connection
    {
    
        //Check the request and returns the response.
    
        NSLog(@"DONE. Received Bytes: %d", [webPortFolio length]);
    
        theXMLPortFolio = [[NSString alloc] 
                          initWithBytes: [webPortFolio mutableBytes] 
                          length:[webPortFolio length] 
                          encoding:NSUTF8StringEncoding];
    
        //---shows the XML---
    
        NSLog(@"shows the XML %@",theXMLPortFolio);
        [theXMLPortFolio release];    
    
        if(xmlParserPortFolio)
        {
            [xmlParserPortFolio release];
        }
        xmlParserPortFolio = [[NSXMLParser alloc] initWithData: webPortFolio];
        [xmlParserPortFolio setDelegate: self];
        [xmlParserPortFolio setShouldResolveExternalEntities:YES];
        [xmlParserPortFolio parse];
        [webPortFolio release];
        [connection release];
    }
    
    //---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:@"GetPortfolioListResult"])
        {
            if (!soapResultsPortFolio)
            {
                soapResultsPortFolio = [[NSMutableString alloc] init];
            }
            elementFoundPortFolio = TRUE;
            NSLog(@"Registration...%@",soapResultsPortFolio);
        }
        else if([elementName isEqualToString:@"your_tag_name"])
        {
            elementFoundPortFolio = TRUE;
        }
        else if([elementName isEqualToString:@"your_tag_name"])
        {
            elementFoundPortFolio = TRUE;
        }
        else if([elementName isEqualToString:@"your_tag_name"])
        {
            elementFoundPortFolio = TRUE;
        }
    
    }
    
    -(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
    {
        if (elementFoundPortFolio)
        {
            [soapResultsPortFolio appendString: string];
        }      
    }
    
    - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
    {
        NSLog(@"Parser error %@ ",[parseError description]);
    }
    
    
    //---when the end of element is found---
    -(void)parser:(NSXMLParser *)parser 
    didEndElement:(NSString *)elementName 
     namespaceURI:(NSString *)namespaceURI 
    qualifiedName:(NSString *)qName
    {
        if ([elementName isEqualToString:@"GetPortfolioListResult"])
        {          
            NSLog(@"display the soap results%@",soapResultsPortFolio);
        }
        else if([elementName isEqualToString:@"your_tag_name"])
        {          
            //Perform required action
        }
        else if([elementName isEqualToString:@"your_tag_name"])
        {
            //Perform required action
        }
        else if([elementName isEqualToString:@"your_tag_name"])
        {
            //Perform required action
        }
    
        [soapResultsPortFolio setString:@""];
        elementFoundPortFolio = FALSE;
    }
    

    编辑:

    if ([elementName isEqualToString:@"media:thumbnail"])
    {
          //Save your URL in an Array
     }
    

    【讨论】:

    • 感谢您的回复,但我明白这一点我不明白如何获取媒体:xml 文档的缩略图
    • @redoc01 : 你正在获取图片的 URL?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2011-11-11
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多