数据顺利。
但问题有土耳其人。
应该是“Düzce”,但是“ÜZCE”来......而且,
“Akçakoca”细胞在视野中“çakoca”来了..
XML网址:http://bykrkc.com/test.xml
XMLParser.m;
#import <Foundation/Foundation.h>
#import "XMLParser.h"
#import "XMLFlightList.h"
@implementation XMLParser
@synthesize ucakList = _ucakList;
NSMutableString *currentNodeContent;
NSXMLParser *parser;
XMLFlightList *currentFlight;
bool isStatus;
-(id) loadXMLByUrl:(NSString *)urlString{
_ucakList = [[NSMutableArray alloc] init];
NSError *error;
NSURL *url =[NSURL URLWithString:urlString];
NSString * dataString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
parser =[[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;
//[NSString stringWithFormat:@"%@",data];
}
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
currentNodeContent = (NSMutableString *)[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:@"sehirler"])
{
currentFlight = [XMLFlightList alloc];
isStatus = YES;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if(isStatus){
if([elementName isEqualToString:@"ad"]){
currentFlight.name = currentNodeContent;
}
}
if([elementName isEqualToString:@"sehirler"]){
[self.ucakList addObject:currentFlight];
currentFlight =nil;
currentNodeContent = nil;
}
}
@end
XMLParser.h;
#import <Foundation/Foundation.h>
@interface XMLParser : NSObject<NSXMLParserDelegate>
@property (strong,readonly) NSMutableArray *ucakList;
-(id) loadXMLByUrl:(NSString *)urlString;
@end
结果;
2013-12-18 15:49:21.505 tableview01[7663:70b] ÇORUM
2013-12-18 15:49:21.507 tableview01[7663:70b] İSTANBUL
2013-12-18 15:49:21.507 tableview01[7663:70b] ÖMERLİ
2013-12-18 15:49:21.508 tableview01[7663:70b] ÜZCE -> Problem True: DÜZCE
2013-12-18 15:49:21.508 tableview01[7663:70b] çakoca -> Problem True: Akçakoca
2013-12-18 15:49:26.156 tableview01[7663:70b] ÇORUM
答案 0 :(得分:1)
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
方法中的问题。对于每个标签,可以多次调用此方法。
尝试更改您的代码:
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (currentNodeContent == nil) currentNodeContent = [NSMutableString new];
[currentNodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
作为替代方案,您可以使用XMLConverter将XML转换为NSDictionary
。
此代码:
[XMLConverter convertXMLURL:[NSURL URLWithString:@"http://bykrkc.com/test.xml"] completion:^(BOOL success, NSMutableDictionary *dictionary, NSError *error) {
NSArray *elements = dictionary[@"DocumentElement"][@"sehirler"];
for (NSDictionary *dic in elements) {
NSLog(@"%@", dic[@"ad"]);
}
}];
执行结果:
2013-12-18 18:00:46.037 test[690:70b] ÇORUM
2013-12-18 18:00:46.038 test[690:70b] İSTANBUL
2013-12-18 18:00:46.038 test[690:70b] ÖMERLİ
2013-12-18 18:00:46.039 test[690:70b] DÜZCE
2013-12-18 18:00:46.039 test[690:70b] Akçakoca