0

我知道它可能看起来像 XML Parsing 帖子的副本,但我真的无法理解节点和委托方法如何表现。我有一个 XML ..

<?xml version="1.0" encoding="UTF-8"?>
<ParticipantService>
    <Response>
        <CourseProperties>
            <CourseTitle>AICC_Flash_Workshop_PPT_to_web_examples</CourseTitle>
            <CourseCode>123456</CourseCode>
            <Availability>Open</Availability>
            <Status>In Progress</Status>
            <ImageLink>HTTP://lmsstaging.2xprime.com/images/inprogress_icon.png</ImageLink>
            <CategoryCode>0</CategoryCode>
            <CategoryDesc>General</CategoryDesc>
        </CourseProperties>
        <CourseProperties>
            <CourseTitle>Behaviours</CourseTitle>
            <CourseCode>OBIUS</CourseCode>
            <Availability>Open</Availability>
            <Status>In Progress</Status>
            <ImageLink>HTTP://lmsstaging.2xprime.com/images/inprogress_icon.png</ImageLink>
            <CategoryCode>0</CategoryCode>
            <CategoryDesc>General</CategoryDesc>
        </CourseProperties>
        <CourseProperties>
            <CourseTitle>Customer Service Skills (Part - one)</CourseTitle>
            <CourseCode>css_1</CourseCode>
            <Availability>Open</Availability>
            <Status>In Progress</Status>
            <ImageLink>HTTP://lmsstaging.2xprime.com/images/inprogress_icon.png</ImageLink>
            <CategoryCode>0</CategoryCode>
            <CategoryDesc>General</CategoryDesc>
        </CourseProperties>

……

我的要求是将相关课程详细信息存储到相应的数组中。所以我声明了六个 nsmutablearray,但对如何从 XMl 中检索数据感到困惑。我正在尝试这种方式

在foundCharacters 方法中,我将字符串的值附加为

videoUrlLink = [NSMutableString stringWithString:string];

在 didEndElement 方法中

if ([elementName isEqualToString:@"CourseTitle"]) {
        [courseDetailList addObject:string];

    } 

但在 XMl 结束时,我只能在数组中存储一个值。如果我在某个地方出错了,请告诉我?

4

1 回答 1

1

我假设您有一个名为 的类Course,并且一个Course对象具有titlecode等的属性availability

制作一个 iVar currentCourse

然后,在您的parser:didStartElement:namespaceURI:qualifiedName:attributes:(注意:确实开始,而不是结束!)方法中:

if ([elementName isEqualToString:@"CourseProperties"]) {
    //create a new course object
    currentCourse = [[Course alloc] init];
}

这为后续课程的所有属性提供了上下文。在该didEndElement:方法中,您基本上对所有课程属性执行此操作:

if ([elementName isEqualToString:@"CourseTitle"]) {
    [currentCourse setTitle:string];
}

最后但并非最不重要的一点是,一旦CourseProperties找到结束标签,将新课程保存在某处(也在didEndElement:):

if ([elementName isEqualToString:@"CourseProperties"]) {
    //create a new course object
    [allMyCourses addObject:currentCourse];
    currentCourse = nil;
}
于 2012-05-21T16:29:42.790 回答