开发者

XML parses with extra whitespace

I have an XML file that looks like this:

<?xml version="1.0"?>
<categories>
   <category id="1">
      <category_name>Banks</category_name>
   </category>
   <category id="4">
      <category_name>Restaurants</category_name>
   </category>
</categories>

The [xmlParser parse] method returns TRUE without errors. But when i print out each element, i get the following results:

Processing characters: 

    Restaurants

Notice the empty line and slight indent of 'Restaurants'. My foundCharacters method prints out the above.

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

if(!currentElementValue)
    currentElementValue = [[NSMutableString alloc] initWithString:string];
else
    [currentElementValue appen开发者_如何转开发dString:string];

NSLog(@"Processing characters: %@", currentElementValue);
}

This causes an indentation of the text in my UITableViewCell - why would this be?

The imageView property is not being set.

Thanks Joe


I used the following to solve this problem

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

It worked


I think what's going on is that whatever the foundCharacters method is writing to isn't getting cleared out on finding the start of a new element, so the whitespace from indenting the tags is still hanging around. If you only clear it when finding the end of an element you would get results like what you describe.


Is there an ignoreWhiteSpace property you can use?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜