views:

344

answers:

2

Hi, I'm currently learn iphone app programming. Its an xml parsing app. In XML, I have an entry as follows:

<text box-opacity="0.75" boxrcolor="1.0" boxgcolor="1.0" boxbcolor="1.0" boxalpha="0.75" textrcolor="0.00" textgcolor="0.00" textbcolor="0.00" textalpha="1.0" show-box="true"> 
      Once upon a fliegen über time, there was an old, mother goat. She had seven little goats whom she loved as any mother loves her children. One day, mother goat wanted to go into the woods to fetch some food, so she called her kids and said,
</text>

And my code looks like:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{   
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] )
{
 if(!currentElementValue)
  currentElementValue = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
 else
  [currentElementValue appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
NSLog(currentElementValue);
}

Its parsing successfully. When running the application, it logs in console as :

2009-12-22 23:07:22.137 StoryBook[50823:20b] Once upon a fliegen
2009-12-22 23:07:22.137 StoryBook[50823:20b] Once upon a fliegenüber time, there was an old, mother goat. She had seven little goats whom she loved as any mother loves her children. One day, mother goat wanted to go into the woods to fetch some food, so she called her kids and said,

Now my question is that if you observe the words "fliegen über" in xml and log, they are different words in xml and single word in log printed on console? Why? How to seperate them?

+3  A: 

No wonder since you are trimming whitespace from the character fragments with stringByTrimmingCharactersInSet:.

Ole Begemann
+2  A: 

OK, I think I understand. You're trimming the whitespace because you don't want all the 50 or so blank spaces before the beginning of the text. Do you need to remove whitespace after the first batch of characters comes in? What about this?

So try this:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{   
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] )
{
        if(!currentElementValue) {
                currentElementValue = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
        } else {
                [currentElementValue appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
        }
}
NSLog(currentElementValue);
}

I didn't compile this, but I think it's right.

Gorm