0

I am creating a iPhone app' who needs to read a XML file

Properties

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *fileName;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSString *date;
@property (nonatomic, strong) NSString *link;

Search the tag (here only for the "title")

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

    // FIRST TAG XML
    if([elementName isEqualToString:@"title"]){
        // GET DATA FROM XML 
        getData = YES; // getData is a bool which is NO initialy
        NSLog(@"Found");
    }
    else
        NSLog(@"Not Found");

}

Getting the data

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    if(getData)
    {
        // CREATION DE L OBJET DEPUIS LE XML
        NSLog(@"This is your desired data = %@",string);
        NSLog(@"Object creation ...");
        Pebkac *peb = [[Pebkac alloc] init];
        NSLog(@"Setup the name ...");
        [peb setName:@"Pebkac"];
        NSLog(@"Small descrption ...");
        [peb setDescription:string];
        [pebkacs addObject:peb];
    }
}

End tag

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

    // XML END TAG
    if ([elementName isEqualToString:@"title"]){
        getData = NO;
    }
}

The bug I've observed is that, sometimes it doesn't get the all text between tags.

IluTov
  • 6,807
  • 6
  • 41
  • 103
Armand
  • 726
  • 11
  • 29

2 Answers2

6

foundChars can be called N times. You always SET the string to the characters you get there. You gotta append the chars again and again and when you get the endTag call, THEN you have to set the result.

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if(!foundChars)
        foundChars = [[NSMutableString alloc] init];
    [foundChars appendString:string];
}

then use it in the end -- example is written to fit your question

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    if(getData)
    {
        // CREATION DE L OBJET DEPUIS LE XML
        NSLog(@"This is your desired data = %@",string);
        NSLog(@"Object creation ...");
        Pebkac *peb = [[Pebkac alloc] init];
        NSLog(@"Setup the name ...");
        [peb setName:@"Pebkac"];
        NSLog(@"Small descrption ...");
        [peb setDescription:foundChars];
        [pebkacs addObject:peb];
    }

    //reset
    foundChars = nil;

    // XML END TAG
    if ([elementName isEqualToString:@"title"]){
        getData = NO;
    }
}
IluTov
  • 6,807
  • 6
  • 41
  • 103
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
0

Method

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

May be called multiple times per element. You need to append string together for each time this method is called per tag, and then set it on the object in:

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

Reference this question/answer for a concise example on how to properly accomplish use of NSXMLParser

Community
  • 1
  • 1
Andy Obusek
  • 12,614
  • 4
  • 41
  • 62