2013-03-02 7 views
6
<root> 
    <table name="radios"> 
     <column name="nameradio">Radio1</column> 
     <column name="logo">http://app.syndicationradio.fr/demo/logo1.png</column> 
     <column name="stream">http://cloud2.syndicationradio.fr:8020</column> 
     <column name="twitter">http://www.twitter.com/#syndicationradio</column> 
     <column name="facebook">http://www.facebook.com/syndicationradio</column> 
     <column name="titre">http://app.syndicationradio.fr/demo/title.xml</column> 
    </table> 
    <table name="radios"> 
     <column name="nameradio">Radio2</column> 
     <column name="logo">http://app.syndicationradio.fr/demo/logo1.png</column> 
     <column name="stream">http://cloud2.syndicationradio.fr:8020</column> 
     <column name="twitter">http://www.twitter.com/#syndicationradio</column> 
     <column name="facebook">http://www.facebook.com/syndicationradio</column> 
     <column name="titre">http://app.syndicationradio.fr/demo/title.xml</column> 
    </table> 
</root> 

Ora per favore c'è qualcuno aiuto per scoprire che, come posso ottenere quelle url dai dati XML utilizzando NSXMLParser o qualsiasi altro parser XML supporre TBXML in IOS?Come posso analizzare questo xml utilizzando NSXMLParser in ios?

Edit: si può anche darmi esempio di libxml parser per questo xml.

Grazie In anticipo.

risposta

18

Prova a questa

- (void)viewDidLoad 

    { 
     NSURL *url = [[NSURL alloc]initWithString:@"yourURL"]; 
     NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:url]; 
     [parser setDelegate:self]; 
     BOOL result = [parser parse]; 
    } 

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 
    { 
     NSLog(@\"Did start element\"); 
    if ([elementName isEqualToString:@"root"]) 
    { 
     NSLog(@"found rootElement"); 
     return; 
    } 
    } 

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
    { 
     NSLog(@"Did end element"); 
     if ([elementName isEqualToString:@"root"]) 
      { 
       NSLog(@"rootelement end"); 
      } 

    } 
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
    { 
     NSString *tagName = @"column"; 

     if([tagName isEqualToString:@"column"]) 
     { 
      NSLog(@"Value %@",string); 
     } 

    } 
+0

Ho uno stringa, non un URL: NSString * urlString = [NSString stringWithFormat: @ "http://www.somewebsite.com/RunPHPtoOutputXML.php?id=%d", ii]; // II è un intero ho quindi eseguire il seguente e sto ricevendo il XML: NSURLRequest * urlRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString]]; NSData * data = [NSURLConnection sendSynchronousRequest: urlRequest returnResponse: & response error: & error]; – user3741598

+0

@ user3741598 Esattamente quello che vuoi chiedere? –

+0

Sono stato tagliato - troppi caratteri - aprirà una nuova domanda. Anche se ho appena trovato una possibile risposta rapida e sporca al lavoro che proverò quando torno a casa. Grazie per avermelo chiesto – user3741598

2

Questo è come è possibile utilizzare NSXMLParser:

Nel dichiarare file .h:

NSMutableData  *webPortFolio; 
NSMutableString  *soapResultsPortFolio; 
NSURLConnection  *conn; 

//---xml parsing--- 

NSXMLParser   *xmlParserPortFolio; 
BOOL    elementFoundPortFolio; 
NSMutableURLRequest *req; 

NSString   *theXMLPortFolio; 
NSString   *strSoapMsg; 
UIAlertView   *alertView; 

Nel file .m utilizzare il seguente codice:

-(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; 
} 
2

Ok hai chiesto per un esempio libxml. L'ho usato in un progetto ma con TBXML anziché NSXMLParser perché questo ha causato importanti problemi di codifica e recupero dei dati.

Per prima cosa è necessario scaricare i file TBXML.m e TBXML.h dal Web e importarli nel progetto. Poi si hanno anche per collegare libxml2.dylib al progetto in link binario con le librerie.

Una volta che questo fatto, si dovrà fare questo per recuperare i dati (in base alla sorgente XML):

NSData *xmlData = [NSData dataWithContentsOfURL:yourURL]; 
TBXML *tbxml = [TBXML newTBXMLWithXMLData:data error:nil]; 
[self getData:tbxml.rootXMLElement]; 

- (void) getData : (TBXMLElement *) element 
{ 
    do { 
     if([[TBXML elementName:element] isEqualToString:@"table"]) 
     { 
      if([[TBXML elementName:element] isEqualToString:@"column"]) 
      { 
       if([[TBXML attributeName:element] isEqualToString:@"nameradio"]) 
       { 
        // You decide what to do here 
       } 
      } 
     } 
     if (element->firstChild) [self getData:element->firstChild]; 
    } while(element = element->nextSibling); 
} 

Probabilmente sarà necessario modificare questo codice, ma qui si hanno tutte le cose fondamentali che bisogno.

+0

thanks.that è il codice così bella e chiara da you.could mi dai il link del TBXML.h e TBXML.m. perché ne ho scaricato uno ma mi mostra qualche errore. – Emon

+0

Con piacere;) [qui] (https://github.com/71squared/TBXML/blob/master/TBXML-Headers/TBXML.h) si trova il file di intestazione, e [qui] (https: // GitHub .com/71squared/TBXML/blob/master/TBXML-Code/TBXML.m) il file di codice. Questa è la versione 1.5, quella che ho usato e ha funzionato bene per me. – Rob

+0

dispiace dirlo, ma qui è un altro problema è 'se ([[tbXml attributeName: elemento] isEqualToString: @ "nameradio"])' mostra alcuni Waring qualcosa di simile 'incompatibile tipo di puntatore TBXMLElement *' – Emon