views:

128

answers:

3

Hi

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 appendString: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

A: 

Is there an ignoreWhiteSpace property you can use?

Shawn Steward
+1  A: 

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.

Nathan Hughes
More specifically, the program is clearing the string on the start of a `category` element (which would be why “Banks” doesn't show up in the example output), but not the start of a `category_name` element.
Peter Hosey
Yes, whenever you encounter the start of a new element you should clear out the string.
Nathan Hughes
+1  A: 

I used the following to solve this problem

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

It worked

joec