开发者

NSXMLParser with UTF8 Data

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.)"


In your connection method that calls the xml, try replacing any problem characters with something like this:

NSString *final = [connRespStr stringByReplacingOccurrencesOfString:@"&" withString:@" and "];

The above line replaces all ampersands with and, maybe use this to replace the singe quotes?

Also, I believe 'error 76' has something to do with not closing an xml tag.

EDIT: Just realized how old this is, maybe the above info will be worthwhile to future searchers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜