Hariprasad
Hariprasad

Reputation: 1084

How to parse special characters in XML for iPad?

I am getting problem while parsing xml files that contains some special characters like single quote,double quote (', "")etc.I am using NSXMLParser's parser:foundCharacters:method to collect characters in my code.

<synctext type = "word" >They raced to the park Arthur pointed to a sign "Whats that say" he asked Zoo said DW Easy as pie</synctext>

When i parse and save the text from above tag of my xml file,the resultant string is appearing,in GDB, as

"\n\t\tThey raced to the park Arthur pointed to a sign \"Whats that say\" he asked Zoo said DW Easy as pie";

Observe there are 2 issues:

1)Unwanted characters at the beginning of the string.

2)The double quotes around Whats that say.

Can any one please help me how to get rid of these unwanted characters and how to read special characters properly.

Upvotes: 1

Views: 805

Answers (2)

Hot Licks
Hot Licks

Reputation: 47699

The parser is apparently returning exactly what's in the string. That is, the XML was coded with the starting tag on one line, a newline, two tabs, and the start of the string. And quotes in the string are obviously there in the original (and it's not clear in at least this example why you'd want to delete them).

But if you want these characters gone then you need to post-process the string. You can use Rams' statement to eliminate the newline and tabs, and stringByReplacingOccurrencesOfString:WithString: to zap the quotes.

(Note that some XML parsers can be instructed to return strings like this with the leading/trailing stuff stripped, but I'm not sure about this one. The quotes will always be there, though.)

Upvotes: 0

Rams
Rams

Reputation: 1751

NSString*string =[string stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n\t"]];

Upvotes: 2

Related Questions