这是后续行动。我已经完成了我想要做的事情。
我有一个ListViewController类和一个存储库模型/对象。为了解析XML,我使用了XPathQuery,Matt Gallagher将libxml2改编为Cocoa(http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html)。
ListViewController跟踪当前的XML路径。当选择表视图中的单元格时,相应的路径将连接到现有路径。 ListViewController创建并推送自己的另一个实例,PerformXMLPathQuery()返回一个数组来填充表格视图单元格。存储库包含一个NSMutableData对象和一个包含当前路径的NSString。两者都是XPath查询所必需的:
NSArray *PerformXMLXPathQuery(NSData *document, NSString *query);
PerformXMLPathQuery为您做了很多后期工作。这是我用它来得到我从XML想:
-(NSArray*)getListFromRepositoryWithPath:(NSString *)path {
// Get the nodes referred to by the path
NSArray *nodes = PerformXMLXPathQuery(repository.data, path);
// Build an array with the node names
NSMutableArray* sublist = [[NSMutableArray alloc] init];
for (NSDictionary* node in nodes) {
for (id key in node) {
if ([key isEqualToString:@"nodeChildArray"]) {
NSArray* subNodes = [node objectForKey:@"nodeChildArray"];
for (NSDictionary* subNode in subNodes) {
for (id subKey in node) {
if ([subKey isEqualToString:@"nodeName"]) {
[sublist addObject:[subNode objectForKey:subKey]];
}
}
}
}
}
// Ignore duplicate entries in the data
if ([sublist count] > 0) {
return [NSArray arrayWithArray:sublist];
}
}
return [NSArray arrayWithArray:sublist];
}
当选择一排,我用didSelectRowAtIndexPath方法准备下一个路径和视图控制器:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *nextXpathQueryString = [self.xpathQueryString copy];
// Build the path string for the next view's XML path query
if ([[nextXpathQueryString lastPathComponent] isEqualToString:@"*"]) {
nextXpathQueryString = [nextXpathQueryString stringByDeletingLastPathComponent];
nextXpathQueryString = [nextXpathQueryString stringByAppendingPathComponent:@ROOTNAME];
}
nextXpathQueryString = [nextXpathQueryString stringByAppendingPathComponent:[repository.currentListToDisplay objectAtIndex:indexPath.row]];
// Navigation logic. Create and push another view controller.
ListViewController *detail = [[ListViewController alloc] init];
// Populate the new ViewController
[detail setRepository:repository];
[detail setTitle:nextXpathQueryString];
[detail setXpathQueryString:nextXpathQueryString];
[nextXpathQueryString release];
UIBarButtonItem *doneButton = [[self navigationItem] rightBarButtonItem];
[[detail navigationItem] setRightBarButtonItem:doneButton animated:YES];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detail animated:YES];
[detail release];
}
很酷的事情是ListViewController的每个实例保留自己的路径字符串和内容数组。没有混搭嵌套的树对象或指针。
基本上我有一个灵活的XML树浏览器的iPhone。感谢堆栈溢出。
新的词汇:钻取http://www.iphonedevsdk.com/forum/iphone-sdk-development/53605-dynamic-navigation-xml-tables.html 我看到如何可以序列化表的数组/ unarchived,但那些意见呢? – slater 2010-10-11 14:27:54