在 iphone / xcode SDK 上遍历 TBXML 有帮助吗?
我有一个 xml 文件,我正在努力遍历它。我会从一开始就让你知道我是 Xcode 新手。我尝试遵循此处的另一个指南:
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
但是,我在 TBXMLElement 之前收到 2 个错误:“预期类型和预期 ')”。
我的 .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;
}
I've got an xml file which i'm struggling to traverse through. I'll let you know from the off i'm new to xcode. I tried following another guide on here:
iPhone TBXML Looping And Parsing Data but for some reason it doesn't seem to work for me.
Here's my XML File:
<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>
I'm trying to traverse through this with TBXML to grab each of the items (I managed to do it with one, but couldn't loop through).
The .h file is:
@interface LoadXML : UIViewController {
IBOutlet UITableView *tblLoadXML;
NSMutableArray *records;
}
@property(nonatomic,retain)NSMutableArray *records;
-(void)loadRecords:(NSString *)records;
-(void)traverseElement:(TBXMLElement *)element;
@end
However I'm getting 2 errors on that saying: `Expected a type and Expected ')' before TBXMLElement.
My .M file has the following:
-(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));
}
I'm probably doing something very stupid, but it's driving me insane! Any help will be much appreciated.
--- This is what I had before, and it works perfectly but only for one record. Basically what i'm trying to do is convert this so I can put each item as an array and pull it out at will:
-(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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据错误,您是否 #include .h 文件中的 TBXML.h?或者,您可以在 .h 文件中使用
@class TBXMLElement;
并在 .m 文件中导入。Based on the error, are you #including TBXML.h from the .h file? Alternatively, you could
@class TBXMLElement;
in the .h file and an import in the .m file.