Why is NSXMLParser picking up this whitespace in the foundCharacters method?
Asked Answered
G

3

7

I'm learning to use the NSXMLParser API for the iOS platform and so far it's very easy to use. I'm having a small problem, however, in the foundCharacters method. As I understand it, it shouldn't pick up any whitespace since the foundIgnorableWhitespace method is supposed to catch that, but it looks like it is. Here's the 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 "time" elements in an array. The problem, however, is it seems to be picking up whitespace in the form of a newline. Here's the printout 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's a snippet of the XML data I'm 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>
     .
     .
     .

Am I misunderstanding how this works?

Thanks in advance for your help!

Gate answered 22/8, 2010 at 2:8 Comment(0)
L
6

The easy solution is to create a didEndElement: method where you set currentElement to kXMLElementOther.

There is a good description of Ignorable White Space at Ignorable White Space. The problem is probably that you do not have a DTD associated with your document. So the parser does not actually know what ignorable white space is. (It is not simply white space between tags, which is probably what you think) So it is simply treating everything as character data.

Litotes answered 22/8, 2010 at 2:55 Comment(0)
L
3
- (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);
}
Lopsided answered 21/2, 2013 at 8:17 Comment(0)
S
2

I found the answer for your question do editing in following piece of code

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

Add this line of code

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

Now reply where ever you find "string" in your function mentioned above with "stringToDisplay"

It worked for me. Hope it will work for you too. Enjoy Coding.

Strobel answered 25/9, 2012 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.