下面是一些示例 XML,显示了我要解析的基本设置。
到目前为止,我可以轻松地提取任务、任务、标题、提示、练习和文本的数据以及type
在练习中获取属性。
但是,我一生都无法弄清楚如何获取包含标签问题的问题块。
<?xml version="1.0" encoding="UTF-8" ?>
<tasks>
<task>
<title>Any ole text goes here</title>
<hint>dont cross busy roads!</hint>
<exercise type="yes_no">
<text>which planet is nearest the sun?</text>
<questions>
<question answer="false">Mars</question>
<question answer="true">Mercury</question>
<question answer="false">Saturn</question>
</questions>
</exercise>
</tasks>
到目前为止,这是我获取数据的方式:
-(void)createTask
{
self.task = [[Task alloc] init];
// grab the task from the loaded xml
NSArray *tasks = [[AppData sharedInstance].XMLTaskDocument.rootElement elementsForName:@"task"];
// cycle through the task and extract its data assigning to appropriate model property
for (GDataXMLElement *task in tasks )
{
NSString *title = nil;
NSArray *titles = [task elementsForName:@"title"];
if ([titles count] > 0)
{
GDataXMLElement *firstTitle = (GDataXMLElement *)[titles objectAtIndex:0];
title = firstTitle.stringValue;
} else continue;
NSString *hint = nil;
NSArray *hints = [task elementsForName:@"hint"];
if ([hints count] > 0)
{
GDataXMLElement *firstHint = (GDataXMLElement *)[hints objectAtIndex:0];
hint = firstHint.stringValue;
} else continue;
NSString *type = nil;
NSString *text = nil;
NSArray *exercises = [task elementsForName:@"exercise"];
if ([exercises count] > 0)
{
type = [(GDataXMLNode *)[[exercises objectAtIndex:0] attributeForName:@"type"] stringValue];
GDataXMLElement *firstText = (GDataXMLElement *)[exercises objectAtIndex:0];
text = firstText.stringValue;
// THIS DOES NOT WORK :-(
NSArray *questions = [task elementsForName:@"questions"];
if ([questions count] > 0)
{
NSLog(@"questions count is: %d", [questions count]);
}
} else continue;
}
}
谁能告诉我如何抓住问题?