Why is NSXMLParser picking up this whitespace in t

2019-06-18 01:28发布

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!

3条回答
叛逆
2楼-- · 2019-06-18 01:41
- (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楼-- · 2019-06-18 01:57

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.

查看更多
聊天终结者
4楼-- · 2019-06-18 01:59

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.

查看更多
登录 后发表回答