我使用的NSXMLParser从URL解析XML(我的代码几乎完全一样在这里 )
一些元件包含特殊字符,如“A”,这将导致一个字LIK安德烈分裂成两个(A和NDRE)。
这里是我的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;}
我敢肯定,这是因为编码没有设置(我认为它需要NSUTF8StringEncoding),但我不知道在哪里/如何应用它。
我的代码[更新] ...休息
- (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]];
}
[更新2]示例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>