如何访问节点 GDataXML 中的节点
下面是一些示例 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;
}
}
谁能告诉我如何抓住问题?
Below is some sample XML showing the basic setup I am trying to parse.
So far I can easily extract the data for tasks, task, title, hint, exercise and text as well as grabbing the attribute type
in exercise.
However I cannot for the life of me figure out how to get the questions block that includes the tag question.
<?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>
Here's how I get the data thus far:
-(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;
}
}
Can anyone tell me hopefully how to grab questions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你犯了一个小错误。您从任务根而不是练习根中调用“elementsForName:@"questions"”。它不起作用,因为“问题”元素不存在于任务元素中,而仅存在于练习元素中。
解决方案应该如下所示:
我希望它能帮助您。
You have made a little mistake. You call the 'elementsForName:@"questions"' from the task root and not from the exercise root. It doesn't work because the "question" element doesn't exist into the task element but only into the exercise element.
The solution should look like that:
I hope it'll help you.