问题
I'm using NSXMLParser to parse an xml from a url (my code is almost exactly the same as here)
Some of the elements contain special characters like "á" which causes a word lik ándre to split into two (á and ndre).
Here is my loadXMLByURL
-(id) loadXMLByURL:(NSString *)urlString{
tickets = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;}
I'm pretty sure it's because the encoding is not set (I think it needs to be NSUTF8StringEncoding) but I'm not sure where/how to apply it.
[UPDATE] Rest of my code...
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:@"ticket"])
{
currentTicket = [Ticket alloc];
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementname isEqualToString:@"name"])
{
currentTicket.name = currentNodeContent;
}
else if ([elementname isEqualToString:@"title"])
{
currentTicket.title = currentNodeContent;
}
else if ([elementname isEqualToString:@"status"])
{
currentTicket.status = currentNodeContent;
}
else if ([elementname isEqualToString:@"ticket"])
{
[tickets addObject:currentTicket];
[currentTicket release];
currentTicket = nil;
[currentNodeContent release];
currentNodeContent = nil;
}
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
[UPDATE 2] Sample xml...
<RB>
<list>
<ticket>
<name>Andrew Ford</name>
<title>3rd release</title>
<status>1</status>
</ticket>
<ticket>
<name>David Jenkins</name>
<title>3rd release</title>
<status>0</status>
</ticket>
<ticket>
<name>Luis gomez ándre</name>
<title>3rd release</title>
<status>1</status>
</ticket>
</list>
</RB>
回答1:
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.
回答2:
Found the problem! It is indeed in found characters. You should change your code to this:
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"found characters: %@", string);
if (!currentNodeContent) {
currentNodeContent = [[NSMutableString alloc] init];
}
[currentNodeContent appendString:string];
}
I was having the same problem before, and the above code has fixed it.
回答3:
Use
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
and to get the string from it, do this:
NSString *theXML = [[NSString alloc] initWithBytes:[data mutableBytes]
length:[data length]
encoding:NSUTF8StringEncoding];
Then you can parse the xml in your NSXMLParserDelegate methods.
Hope this helps.
来源:https://stackoverflow.com/questions/10215221/nsxmlparser-cant-parse-special-characters-accents