I'm having a really hard time trying to figure this out and would really appreciate any help.
I'm trying to parse a utf 8 string with NSXMLParser but it won't work.
here is my string
<?xml version="1.0" encoding="UTF-8"?><host><type>mac</type><port>62181</port><address>192.168.1.159</address><hostname>Samuel’s%20Mac%20Book</hostname><username>samuelw</username></host>
and here the parsing code
- (void) parse:(NSString*)XMLEncodedString withLength:(int)l_length {
#ifndef NDEBUG
NSLog(@"Received lookup %@",XMLEncodedString);
#endif
NSData* data=[XMLEncodedString dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self]; // The parser calls methods in this class
[parser setShouldProcessNamespaces:NO]; // We don't care about namespaces
[parser setShouldReportNamespacePrefixes:NO]; //
[parser setShouldResolveExternalEntities:NO]; // We just want data, no other stuff
[parser parse]; // Parse that data..
[parser release];
}
the XMLEncodedString is constructed like this
- (void) checkForReceive {
//NSLog(@"listener: waiting to recvfrom...\n");
addr_len = sizeof their_addr;
if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
return;
}
buf[numbytes] = '\0';
NSString * string = [[NSString alloc] initWithUTF8String:buf];
[self parse:string withLength:numbytes];
[string release];
}
my problem is coming from the Unicode Character 'RIGHT SINGLE QUOTATION MARK' (U+2019) that I have in my string and I am getting this error:
2010-07-19 17:13:35.734 SwypeSendForMac2[34354:4233] Error Domain=NSXMLParserErrorDomain Code=73 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 73.)" 2010-07-19 17:13:35.736 SwypeSendForMac2[34354:4233] Error Domain=NSXMLParserErrorDomain Code=76 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 76.)"