How can i Parse this xml using NSXMLParser in ios?

后端 未结 3 1843
日久生厌
日久生厌 2020-12-16 05:08

    Radio1http://app.syndicat         


        
      
      
      
3条回答
  •  一向
    一向 (楼主)
    2020-12-16 05:38

    This is how you can use NSXMLParser :

    In your .h file declare :

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

    In your .m file use the following code:

    -(void)callURL
    {
    
         //Your logic to call URL.
    
         conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
         if (conn)
         {
             webPortFolio = [[NSMutableData data] retain];
         }
    }
    And to handle the response you can use following functions :
    
    -(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:@"your_tag_name"])
        {
            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:@"your_tag_name"])
        {          
            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;
    }
    

提交回复
热议问题