2012-11-21 57 views
1

我有一個XML如何閱讀XML並在標籤上顯示數據?

<?xml version="1.0" encoding="UTF-8"?> 
<drivers> 

<driver> 
<img><![CDATA[45djx96.jpg]]></img> 
<name><![CDATA[Alonso]]></name> 
<teamname><![CDATA[farari]]></teamname> 
<ref><![CDATA[45djx96]]></ref> 
</driver> 

<driver> 
<img><![CDATA[1236.jpg]]></img> 
<name><![CDATA[Alonso2]]></name> 
<teamname><![CDATA[farari2]]></teamname> 
<ref><![CDATA[1236]]></ref> 
</driver> 

<driver> 
<img><![CDATA[1245FGt.jpg]]></img> 
<name><![CDATA[Alonso3]]></name> 
<teamname><![CDATA[farari3]]></teamname> 
<ref><![CDATA[1245FGt]]></ref> 
</driver> 

</drivers> 

我想說明的只是細節

意味着1236上UILable,名字& teamname。

我正在使用webservice第一次。我成功地在控制檯上捕獲XML,但無法讀取XML並在UILable上顯示名稱&團隊名稱的值。

現在xml顯示在控制檯上,但無法解析。

有什麼想法?

在此先感謝。

回答

1

使用TouchXML您可以到陣列分析這個數據,並且還單獨記錄字符串我只是張貼一些關於它的代碼

CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:data options:0 error:nil] autorelease]; 
NSArray *nodes = [doc nodesForXPath:@"//driver" error:nil]; 

for (CXMLElement *node in nodes) {  
    // Create Object Of Bean Class. if required 

    for(int counter = 0; counter < [node childCount]; counter++) {   
     //Save Data in The Bean Class and Add In the Array. 

     if ([[[node childAtIndex:counter] name] isEqualToString:@"teamname"]) {     
      NSString *string = [[node childAtIndex:counter] stringValue]; 
          yourLable.Text = string; 
      //    NSLog(@"\n\n Title %@",string); 

     } 
     else if ([[[node childAtIndex:counter] name] isEqualToString:@"ref"]) {     
      NSString *string = [[node childAtIndex:counter] stringValue]; 
      //    NSLog(@"\n\n Ref %@",string); 
     } 
} 

}

爲TouchXML請參見本教程和例子也

  1. TouchXML

  2. iphone-sdk-tutorial-building-an-advanced-rss-reader-using-touchxml

+0

https://github.com/TouchCode/TouchXML它演示:) –

+0

感謝回答,但它不是工作變動做我。它說未知的接收器「CXMLDocument」 –

+0

@AreebaKhan yes隊友首先下載touchXML庫並放入您的項目並導入頭文件,如#import「CXMLDocument.h」然後它的工作和更詳細的信息請參閱鏈接教程和試用Demo隊友.. :) –

0

好,的NSXMLParser類應該按門鈴,我認爲。 但是,它並不那麼方便,您可以找到許多更好的解析器,例如TBXML(one of branches)。 此外,通常建議使用JSON而不是XML,因爲它更輕且更易於解析。最常見的JSON解析器庫是SBJSON。

1

對於解析,您可以使用需要以下類的TBXML。 TBXML.h,TBXML.m & NSDataAdditions.h,NSDataAdditions.m。

我已經給了一個樣本example.You可以根據你的代碼

NSString *cStr = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><SetPatientBPXMLResponse xmlns=\"http://webservice.cruxmed.com/\"><SetPatientBPXMLResult><response><state><![CDATA[45djx96.jpg]]></state></response></SetPatientBPXMLResult></SetPatientBPXMLResponse></soap:Body></soap:Envelope>"]; 

    TBXML *tbxmlObj = [[TBXML alloc] initWithXMLString:cStr]; 

    // Obtain root element 
    TBXMLElement * root = tbxmlObj.rootXMLElement; 
    //------------------------------------------- 
    if (root) 
    { 
     //------------------------------------------- 
     // search for the first NewDataSet element within the root element's children 
     // instantiate an NewDataSet objectGetDataInXMLFromStoredProcedureResponse 
     TBXMLElement * SoapBody = [TBXML childElementNamed:@"soap:Body" parentElement:root]; 

     TBXMLElement * SoapResponse = [TBXML childElementNamed:@"SetPatientBPXMLResponse" parentElement:SoapBody]; 

     TBXMLElement * SoapResult = [TBXML childElementNamed:@"SetPatientBPXMLResult" parentElement:SoapResponse]; 

     //TBXMLElement * NewDataSet1 = [TBXML childElementNamed:@"response" parentElement:SoapResult]; 

     TBXMLElement * NewDataSet = [TBXML childElementNamed:@"response" parentElement:SoapResult]; 
     // if an _wspGetChartTemplateByAgent element was found 
     while (NewDataSet != nil) { 


      //cAccountName,fOrdTotTaxDEx,fOrdTotTax,; 

      // instantiate a Order object 


      // find the iInvoiceId 
      TBXMLElement * AutoIndex = [TBXML childElementNamed:@"state" parentElement:NewDataSet]; 

      if (AutoIndex != nil) 
      { 
       NSString* iVal=[TBXML textForElement:AutoIndex] ; 
       NSLog(@"iVal:%@",iVal); 
      } 
      // find the next sibling element named "_wspGetChartTemplateByAgent" 
      NewDataSet = [TBXML nextSiblingNamed:@"response" searchFromElement:NewDataSet]; 
     } 
    } 
相關問題