0

我正在尝试解析 xml 文件,但无法检索其中的所有对象。

我的xml是

<exam>
<question s="1" a="2" m="1"  x="1">
    <q>question</q>
    <o p="">question</o>
    <o p="">question</o>
    <o p="">question</o>
    <o p="">question</o>

</question>
<question s="2" a="1,3,5" m="3" p="1.2.jpg" x="1">
    <q>question</q>
    <o p="">question</o>
    <o p="">question</o>
    <o p="">question</o>
    <o p="">question</o>
    <o p="">question</o>
    <o p="">question</o>
</question>
</exam>

如何检索所有数据,例如“s="2"”和“<.op=>question<./o>”

4

2 回答 2

0

使用GDataXML & 考虑xmlData将是 XML 字符串NSData表示。

GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData error:&error];

NSArray *dataArray = [doc nodesForXPath:@"//exam/question" error:nil];

  for (GDataXMLElement *element in dataArray) {

        NSArray * question = [element elementsForName:@"s"];

        if (question.count > 0) {
            GDataXMLElement *questionElement = (GDataXMLElement *) [question objectAtIndex:0];
            questionString = [questionElement stringValue];
        }
}

在此处查看更多详细信息

于 2013-03-27T21:55:49.307 回答
0

感谢您的帮助。这是我在朋友的帮助下找到的解决方案(抱歉变量名选择不当):

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    currentElement = elementName;
    if([elementName isEqualToString:@"item"])
    {
        qes = [[Qes alloc] init];
        qes.s= [attributeDict objectForKey:@"s"];

    }

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if([currentElement isEqualToString:@"q"])
        [qes.question appendString:string];
    if([currentElement isEqualToString:@"o"])
    {
        [qes.answers addObject:string];
    }

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"item"])
    {
        [quests addObject:qes];

    }
}
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    for (Qes *s in quests) {
        NSLog(s.question);
        NSLog(@"s-----");
        NSLog(s.s);

        for (NSString *w in s.answers) {
             NSLog(w);

        }

    }

}
于 2013-05-06T12:59:08.807 回答