2013-03-02 78 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> 

現在請有沒有人幫助找出,我怎麼能從XML數據使用NSXMLParser或任何其他XML解析器假設TBXML在IOS IOS?我如何解析這個xml在NS中使用NSXMLParser?

編輯:你也可以給我舉例libxml解析器這個XML的。

在此先感謝。

回答

18

嘗試這一

- (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

我有一個字符串,而不是URL:的NSString * urlString = [NSString的stringWithFormat:@ 「http://www.somewebsite.com/RunPHPtoOutputXML.php?id=%d」,II]; // ii是一個整數,然後運行以下命令獲取XML:NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];我們可以使用NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; – user3741598 2014-07-14 03:17:08

+0

@ user3741598究竟你想問什麼? – 2014-07-14 05:01:51

+0

我被切斷了 - 太多人物 - 會開啓一個新問題。雖然我剛剛想出了一個潛在的快速,骯髒的可能的工作答案,我會回家時嘗試。感謝您的詢問。 – user3741598 2014-07-14 14:03:17

2

這是你如何使用的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]; 
    } 
} 
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

好你問例如一個libxml的例子。我在一個項目中使用它,但用TBXML而不是NSXMLParser,因爲這個項目導致了編碼和數據檢索的重要問題。

首先,您必須從網上下載TBXML.mTBXML.h文件並將它們導入到您的項目中。然後,你還必須在鏈接二進制與圖書館鏈接libxml2.dylib到您的項目。

一旦這樣做了,你將不得不這樣做是爲了獲取你的數據(根據您的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); 
} 

你可能要改變這種代碼,但在這裏,你有你的基本的東西需要。

+0

thanks.that是you.could你給我TBXML.h和TBXML.m的鏈接,這樣漂亮,清晰的代碼。因爲我已經下載了一個,但它顯示出一些錯誤。 – Emon 2013-03-02 10:36:54

+0

有了快感;)[這裏](https://github.com/71squared/TBXML/blob/master/TBXML-Headers/TBXML.h),你會發現頭文件和[這裏](https://開頭的github .com/71squared/TBXML/blob/master/TBXML-Code/TBXML.m)代碼文件。這是1.5版本,我使用並且爲我工作得很好。 – Rob 2013-03-02 10:42:44

+0

抱歉地說,但這裏是另外一個問題是'如果([TBXML的attributeName:元素] isEqualToString:@「nameradio」])'顯示出一些華林類似的東西'兼容的指針類型TBXMLElement *' – Emon 2013-03-02 11:52:31