0

I muste parse an XML file that contains some courses. Foreach course I must create an object and at the end to the tag I must add in array, so I can create tableview by array of objects. But I dont't be able to determine the end of course to add to the array. How to be able to do?

XML file is:

<?xml version="1.0" encoding="UTF-8"?>
<courses>
    <course>
        <id>1</id>
        <title>Introduzione al Java</title>
        <description>Corso base java, definizione di classe, oggetti ecc</description>
        <creation_date>2014-01-19 22:52:12</creation_date>
        <teachers>
            <teacher>Paolo</teacher>
            <teacher>Fabio</teacher>
        </teachers>
    </course>
    <course>
        <id>2</id>
        <title>Concetti Avanzati Java</title>
        <description>Corso avanzato java, prove pratiche su sistemi complessi</description>
        <creation_date>2014-02-22 20:52:12</creation_date>
        <teachers>
            <teacher>Giorgio Magherini</teacher>
        </teachers>
    </course>
    <course>
        <id>3</id>
        <title>Concetti Avanzati PHP</title>
        <description>Corso PHP avanzato, prove di verifica su siti online</description>
        <creation_date>2013-01-19 22:52:12</creation_date>
        <teachers>
            <teacher>Giorgio</teacher>
        </teachers>
    </course>
</courses>

I use NSXMLParserDelegate and the methods used are:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
   attributes:(NSDictionary *)attributeDict {

    if ([elementName isEqualToString:@"courses"]) {
        arrayCorsiDisponibili = [[NSMutableArray alloc]init];// I allocate and init arrayCourseAvaiable
         self.stringaAppoggio = @"courses";
    }
    else if ([elementName isEqualToString:@"course"]){

        CourseAvailable *corsoDisponibile = [[CourseAvailable alloc]init]; //I allocate and init object
        self.courseAvailableAppoggio = corsoDisponibile;
        self.stringaAppoggio = @"course";
    }
    else if ([elementName isEqualToString:@"id"]){

    }
    else if([elementName isEqualToString:@"title"]){

    }
    else if([elementName isEqualToString:@"description"]) {

        self.stringaAppoggio = @"description";
    }
    else if([elementName isEqualToString:@"creation_date"]) {

        self.stringaAppoggio = @"creation_date";
    }
    else if([elementName isEqualToString:@"teacher"]) {
         self.stringaAppoggio = @"teacher";
    }
}

And

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if ([self.stringaAppoggio isEqualToString:@"\n"])
    {
        return;
    }
    if ([self.stringaAppoggio isEqualToString:@""])
    {
        return;
    }
    else if ([self.stringaAppoggio isEqualToString:@"courses"])
    {
        return;
    }
    else if ([self.stringaAppoggio isEqualToString:@"course"])
    {
        return;
    }
    else if ([self.stringaAppoggio isEqualToString:@"id"])
    {
        self.courseAvailableAppoggio.idCorso = (NSNumber *)string;
        return;
    }
    else if([self.stringaAppoggio isEqualToString:@"title"])
    {
        self.courseAvailableAppoggio.titolo = string;
        return;
    }
    else if([self.stringaAppoggio isEqualToString:@"description"])
    {
        self.courseAvailableAppoggio.descrizione = string;
        return;
    }
    else if([self.stringaAppoggio isEqualToString:@"creation_date"])
    {
        self.courseAvailableAppoggio.dataCreazione = string;
        return;
    }
    else if([self.stringaAppoggio isEqualToString:@"teacher"])
    {
        [self.courseAvailableAppoggio.insegnante addObject:string];
        return;
    }
    [arrayCorsiDisponibili addObject:self.courseAvailableAppoggio];
}

How to be able to determine the end to add object in array? Thanks

3 Answers 3

1

I used this lib. with enumerate in block https://github.com/ZaBlanc/RaptureXML

Sign up to request clarification or add additional context in comments.

Comments

0

Just check for the tags you're interested in in

– parser:didEndElement:namespaceURI:qualifiedName:

method.

Comments

0

Follow the following links which have detail implementation of XML parsing. link1 and link2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.