Why does NSXMLParser use this space in the foundCharacters method?

I am studying the use of the NSXMLParser API for the iOS platform and is still very easy to use. However, I have a little problem in the foundCharacters method. As far as I understand, it should not select any spaces, since the foundIgnorableWhitespace method should catch this, but it looks like it is. Here is my code ...

  - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { //We're at the start of a new data feed if([elementName isEqualToString:@"data"]) { if(listOfTimes != nil) [listOfTimes release]; listOfTimes = [[NSMutableArray alloc] init]; } else if ( [elementName isEqualToString:@"start-valid-time"]) { currentElementType = kXMLElementTime; return; } else { currentElementType = kXMLElementOther; } //--------------------------------------------------------------------- - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if(currentElementType == kXMLElementTime) { //We don't want anymore than three times if ([listOfTimes count] >= 3) return; [listOfTimes addObject:string]; } } 

It basically stores three "temporary" elements in an array. The problem, however, is that it seems to be collecting spaces as a new line. Here's a listing of the array in the console ...

 Printing description of listOfTimes: ( "2010-08-21T22:00:00-05:00", "\n ", "2010-08-22T01:00:00-05:00" ) 

and here is a piece of XML data that I am processing ...

  <time-layout time-coordinate="local" summarization="none"> <layout-key>k-p3h-n40-1</layout-key> <start-valid-time>2010-08-21T22:00:00-05:00</start-valid-time> <start-valid-time>2010-08-22T01:00:00-05:00</start-valid-time> <start-valid-time>2010-08-22T04:00:00-05:00</start-valid-time> <start-valid-time>2010-08-22T07:00:00-05:00</start-valid-time> <start-valid-time>2010-08-22T10:00:00-05:00</start-valid-time> . . . 

I donโ€™t understand how it works?

Thanks in advance for your help!

+7
ios iphone ipad nsxmlparser
source share
3 answers

A simple solution is to create a didEndElement: method in which you set currentElement to kXMLElementOther .

There is a good description of Ignorable White Space in Minor Empty Space . Probably the problem is that you do not have a DTD associated with your document. Thus, the parser does not actually know what ignorant empty space is. (This is not just a space between tags, which is probably what you think) That way, it just treats everything as character data.

+5
source share
 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { //whatever data i am getting from node i am appending it to the nodecontent variable [nodecontent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; NSLog(@"node content = %@",nodecontent); } 
+3
source share

I found the answer to your question by editing in the following code snippet

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

Add this line of code

 NSString *stringToDisplay = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

Now answer wherever you find the "string" in your function mentioned above with "stringToDisplay"

It worked for me. Hope this works for you too. Enjoy the coding.

+2
source share

All Articles