NSXMLParser: Unexpected result with non-ASCII characters
The parser:foundCharacters:
delegate method can be called more than
once for a single XML element. In your example, it would be called
once with "Contabilit", and once with "à nazionale".
Therefore you have to concatenate
the found strings from didStartElement
until didEndElement
.
Here is a very simply example how this can be done. Of course it gets
more complicated if you have nested XML elements.
Add a property for the current element string to your class:
var currentElement : String?
And then implement the delegate methods like this:
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
// If a "Name" element started (which you are interested in), set
// currentElement to an empty string, so that the found characters
// can be collected. Otherwise set it to nil.
if elementName == "Name" {
currentElement = ""
} else {
currentElement = nil
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
// If the "Name" element ended, get the collected string and
// append it to your list.
if elementName == "Name" {
if let name = currentElement {
println(name)
myList.append(name)
}
}
currentElement = nil
}
func parser(parser: NSXMLParser, foundCharacters string: String?) {
// If currentElement is not nil, append the found characters to it:
currentElement? += string ?? ""
}
NSXMLParser can't parse special characters (accents)
I would load the url to an NSString
and then convert like this.
-(id) loadXMLByURL:(NSString *)urlString{
tickets = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSError *error;
NSString * dataString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;
}
EDIT:
Part of the problem may be that your parser:foundCharacters:
method is assigning to your currentNodeContent
instead of appending. See the Apple Doc at the following link.
http://developer.apple.com/library/ios/#documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html
From the doc:
Because string may be only part of the total character content for the current element, you should append it to the current accumulation of characters until the element changes.
iOS NSXMLParser can't read non-english chars?
You can use below code :
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSString *correctString = [NSString stringWithCString:[string cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
}
Hope this will help you.
All the best !!!
Dealing with special characters like (ö, Ä, é, ß) in iOS
just use NSUTF8StringEncoding instead
of NSStringEncodingConversionAllowLossy
like....
NSString *jsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:URL] encoding:NSStringEncodingConversionAllowLossy error:nil];
Appending a String to NSMutableString in Swift
ftitle = NSMutableString.alloc()
is not a correct way to assign an empty string. It allocates the NSMutableString
without initializing it. NSMutableString
is a class
cluster, so this can cause all kinds of strange errors.
In Swift, you just would do
ftitle = NSMutableString()
or simply
ftitle = ""
For the same reason,
elements = NSMutableDictionary.alloc()
is wrong and should be
elements = NSMutableDictionary()
Special Characters in Webservice Request
Since I control the webservice and the platforms consuming it, I was able to develop my own convention to deal with special characters. I know that for the content I'm sending right now, it's very unlikely I'd ever have a value containing a double underscore, and underscores seem to be legal for everything I've seen so far, so I developed a system where I spell out a character and surround it with underscores. So I'd have something like
...<username>__QUOTE__Frank__QUOTE__</username>...
in my new system. I can look for special characters just before sending a request from my app or a response from the webservice, and do the simple string replacements. Then as soon as I receive the XML, look for the replacements, and turn them back into the special characters before using the data (such as showing on a screen or saving into a database).
I don't particularly like this solution. I would much rather subscribe to a more universal convention. Since I can't seem to find that convention, if such convention exists, this will do for now.
Related Topics
Avcapturesession and Background Audio iOS 7
How to Debug an iOS Extension (.Appex)
Can Not Save File Inside Tmp Directory
Xcode 6 Isn't Autocompleting in Swift
How to Use Apple's New .P8 Certificate for Apns in Firebase Console
Swift iOS Check If Remote Push Notifications Are Enabled in iOS9 and iOS10
Hiding the Tabbar and Removing the Space
How to Authenticate the Gklocalplayer on My 'Third Party Server'
Ios: Custom Permission Alert View Text
Offscreen Uitableviewcells (For Size Calculations) Not Respecting Size Class
Iphone:Daily Local Notifications
Type 'Any' Has No Subscript Members (Firebase)
Nsxmlparser: Unexpected Result with Non-Ascii Characters
Save and Load from Keychain | Swift
Programmatically Add Centerx/Centery Constraints
Uitableviewcell Checkmark to Be Toggled on and Off When Tapped