0

I'm using the NSXMLParser however i dont quite understand how to display items from a xml correctly. E.g. i have this in the xml parser:

- (void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
   namespaceURI:(NSString *)namespaceURI
  qualifiedName:(NSString *)qName
     attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"level4"]) {
        //add a book object to the array at the index determined by bookCount
        self.bookCount++;
        [books addObject:[[book alloc]init]];
        //[books insertObject:[[book alloc]init] atIndex:self.bookCount];
    }   
    if ([elementName isEqualToString:@"module"]) {
        isFirstName = YES;
    }
    if ([elementName isEqualToString:@"moduleTitle"]) {
        isLastName = YES;
    }
    if ([elementName isEqualToString:@"semester"]) {
        isTitle = YES;
    }
    if ([elementName isEqualToString:@"assessmentName"]) {
        isGenre = YES;
    }
}

This is my xml

<myCourse>
    <courseName>BEng Mobile and Web Computing</courseName>
    <courseStructure>
        <level4>
            <module>
                <moduleCode>ECSC401</moduleCode>
                <moduleTitle>Programming Methodology</moduleTitle>
                <credits>15</credits>
                <semester>1</semester>
                <assessmentDetails>
                    <assessment>
                        <assessmentName>Test1</assessmentName>
                        <assessmentType>Coursework</assessmentType>
                        <assessmentWeighting>30</assessmentWeighting>
                        <assessmentDueDate/>
                    </assessment>
                    <assessment>
                        <assessmentName>Coursework</assessmentName>
                        <assessmentType>Coursework</assessmentType>
                        <assessmentWeighting>40</assessmentWeighting>
                        <assessmentDueDate/>
                    </assessment>
                    <assessment>
                        <assessmentName>Test2</assessmentName>
                        <assessmentType>Coursework</assessmentType>
                        <assessmentWeighting>30</assessmentWeighting>
                        <assessmentDueDate/>
                    </assessment>
                </assessmentDetails>
            </module>
        </level4>
    </courseStructure>
</myCourse>

(it continues level 1,level2,level3 using the same format). I simply want to display all the modules under the 'level4' hierarchy - how can i do this?/what am i doing wrong? the items are displaying just not the right ones ..

The Pixel Developer
  • 13,282
  • 10
  • 43
  • 60
user763917
  • 21
  • 1
  • 3
  • Possible duplicate of [Best approach for XML parsing on the iPhone](http://stackoverflow.com/questions/842292/best-approach-for-xml-parsing-on-the-iphone) or [Simple XML Parsing](http://stackoverflow.com/questions/3395237/simple-xml-parsing) or [Parsing XML Code on iPhone](http://stackoverflow.com/questions/3616447/parsing-xml-code-on-iphone-sdk) or [Parsing XML in Cocoa](http://stackoverflow.com/questions/1089737/parsing-xml-in-cocoa) or [NSXMLParser on iPhone how do I use it?](http://stackoverflow.com/questions/2964503/). Take your pick. – jscs May 21 '11 at 17:05

2 Answers2

1

First of all you have to create one NSMutableDictionary. When you get the tag in didStartElement Method initiate this Dictionary.And also set one flag.

And in Found character method check that flag, if It is then store that tagValues and in didEndElement store all the values in this Dictionary when you find .

You can find all levels values using this. Hope this will help you.

Jane
  • 288
  • 1
  • 3
  • 18
0

Use both methods:

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

and

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

For parsing.

Arvind
  • 543
  • 1
  • 5
  • 17
  • Still does not answer my question, why is it reading only the last element .. and how can i read only elements from to – user763917 May 21 '11 at 12:11