2011-08-18 27 views
2

我有一個XML文件,我正在努力穿越。我會讓你知道從我是新的xcode。我試過下面就這裏的另一個導向:在iphone/xcode SDK上遍歷TBXML的幫助?

iPhone TBXML Looping And Parsing Data但出於某種原因,它似乎並沒有爲我工作。

這裏是我的XML文件:

<Locations action="Request" version="1.0"> 
    <location> 
     <CompanyID>44502</CompanyID> 
     <CompanyName>Holiday Inn</CompanyName> 
     <Distance>3.58km/2.23miles</Distance> 
     <tabs> 
      <tab>General Information</tab> 
      <tab>Add Review</tab> 
     </tabs> 
     <Address> 
      <Address1>Holiday Inn Reading - South</Address1> 
      <Address2>500 Basingstoke Road</Address2> 
      <Address3/> 
      <PostTown>Reading</PostTown> 
      <County/> 
      <PostCode>RG2 0SL</PostCode> 
     </Address> 
    </location> 
</Locations> 

我想通過這次與TBXML遍歷抓住每一個項目(我設法用一個做到這一點,但不能環通)。

.h文件是:

@interface LoadXML : UIViewController { 

    IBOutlet UITableView *tblLoadXML; 
    NSMutableArray *records; 
    } 
@property(nonatomic,retain)NSMutableArray *records; 

-(void)loadRecords:(NSString *)records; 
-(void)traverseElement:(TBXMLElement *)element; 

@end 

不過我越來越對俗話說2個錯誤:`TBXMLElement之前預期的類型和預期 ')'。

我.m文件有以下幾點:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (Cell == nil) { 
     Cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 


    Cell.text = [records objectAtIndex:indexPath.row]; 

} 

- (void)loadRecords:(NSString *)records { 
    NSString *someXML = @"http://de0937/directenquirieswebapplicationv3.0/mobilesearch/what/0/49424/342/0/Reading/Hotels%20and%20Inns/0/0/0/0/0/search.aspx"; 
    TBXML *tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:someXML]] retain]; 

    records = [NSMutableArray array]; 
    [records retain]; 

    if (tbxml.rootXMLElement) 
     [self traverseElement:tbxml.rootXMLElement]; 
    [tbxml release]; 
} 

- (void) traverseElement:(TBXMLElement *)element { 
    do { 
     if (element->firstChild) 
      [self traverseElement:element->firstChild]; 

     if ([[TBXML elementName:element] isEqualToString:@"location"]) { 
      TBXMLElement *name = [TBXML childElementNamed:@"CompanyName" parentElement:element]; 
      TBXMLElement *distance = [TBXML childElementNamed:@"Distance" parentElement:element]; 

      TBXMLElement *id = [TBXML childElementNamed:@"ID" parentElement:element]; 

      [records addObject:[NSArray arrayWithObjects: 
           [TBXML textForElement:name], 
           [TBXML textForElement:distance], 
           [TBXML textForElement:id],nil]]; 
     } 
    } while ((element = element->nextSibling)); 
} 

我可能做一些非常愚蠢的,但它的駕駛我瘋了!任何幫助都感激不盡。

---這是我以前的,它完美的作品,但只有一個記錄。基本上我試圖做的是轉換這讓我可以把每個項目作爲一個數組然後將其拉出隨意:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (Cell == nil) { 
     Cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 


    TBXML * XML = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.tomstest.info/ios/results.xml"]] retain]; 

    TBXMLElement *rootXML = XML.rootXMLElement; 
    TBXMLElement *results = [TBXML childElementNamed:@"location" parentElement:rootXML]; 
    TBXMLElement *WOEID = [TBXML childElementNamed:@"CompanyName" parentElement:results]; 
    NSString *woeid = [TBXML textForElement:WOEID]; 


    Cell.text = woeid; 
    return Cell; 

} 

回答

1

基於錯誤,你從.H#包括TBXML.h文件?或者,您可以在.h文件中輸入@class TBXMLElement;,並在.m文件中輸入。

+0

嗨,謝謝你。我在.m中導入,而在.h中沒有任何內容,我已將@class放入並且現在編譯。但是,如果出現SIGBRT錯誤,並將main.m中的斷點放在int retVal = UIApplicationMain(argc,argv,nil,nil); – TMB87

+0

聽起來像我的答案幫助。你應該把它投票。 :-)您可以提供有關錯誤的更多信息?把堆棧跟蹤和錯誤。你記得添加libz.dylib嗎? –

+0

它的幫助,我沒有代表投票它,我害怕。錯誤說:int retVal = UIApplicationMain(argc,argv,nil,nil);帶有SIGBART錯誤的 已經崩潰。我認爲這是一個紅鯡魚。你知道我是否已經完成了我的零件。 – TMB87