我正在解析这样的数据。
parserSignIn = [[NSXMLParser alloc]initWithData:_responseData];
[parserSignIn setDelegate:self];
[parserSignIn parse];
之后
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
tagName = [[NSString alloc] initWithString:elementName];
if (parser == parserSignIn) {
if ([tagName isEqualToString:@"MemberDetail"]) {
NSLog(@"value is %@", elementName);
NSManagedObjectContext *managedObjectContext = [database managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"MemberDetails" inManagedObjectContext:managedObjectContext];
memberDetails = [[MemberDetails alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:managedObjectContext];
}
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"FOUND %@", string);
if ([tagName isEqualToString:@"InvalidLogin"]) {
isSignIn = NO;
}else{
NSLog(@"NOT INVALID");
isSignIn = YES;
if ([tagName isEqualToString:@"MemberId"]) {
memberDetails.memberId = string;
}else if ([tagName isEqualToString:@"FirstName"]){
memberDetails.firstname = string;
}else if ([tagName isEqualToString:@"LastName"]){
memberDetails.lastname = string;
}
else if ([tagName isEqualToString:@"Email"]){
memberDetails.email = string;
}
NSLog(@"DATA is %@", memberDetails);
}
}
其中MemberDetails.h是
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface MemberDetails : NSManagedObject
@property (nonatomic, retain) NSString * memberId;
@property (nonatomic, retain) NSString * firstname;
@property (nonatomic, retain) NSString * lastname;
@property (nonatomic, retain) NSString * email;
MemberDetails.m is
@dynamic memberId;
@dynamic firstname;
@dynamic lastname;
@dynamic email;
当我打印数据时,使用NSLog到MemberDetails对象
0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
email = nil;
firstname = nil;
lastname = nil;
memberId = nil;
})
2013-09-15 16:10:33.540 ECP[24094:c07] FOUND 10758
2013-09-15 16:10:33.541 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.541 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
email = nil;
firstname = nil;
lastname = nil;
memberId = 10758;
})
2013-09-15 16:10:33.543 ECP[24094:c07] FOUND
2013-09-15 16:10:33.543 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.544 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
email = nil;
firstname = nil;
lastname = nil;
memberId = "\n ";
})
2013-09-15 16:10:33.544 ECP[24094:c07] FOUND Amit
2013-09-15 16:10:33.545 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.545 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
email = nil;
firstname = Duaan;
lastname = nil;
memberId = "\n ";
})
2013-09-15 16:10:33.545 ECP[24094:c07] FOUND
2013-09-15 16:10:33.545 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.546 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
email = nil;
firstname = "\n ";
lastname = nil;
memberId = "\n ";
})
2013-09-15 16:10:33.548 ECP[24094:c07] FOUND Metha
2013-09-15 16:10:33.548 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.548 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
email = nil;
firstname = "\n ";
lastname = Rajput;
memberId = "\n ";
})
2013-09-15 16:10:33.548 ECP[24094:c07] FOUND
2013-09-15 16:10:33.549 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.549 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
email = nil;
firstname = "\n ";
lastname = "\n ";
memberId = "\n ";
})
2013-09-15 16:10:33.549 ECP[24094:c07] FOUND amit@sevdotcom.ae
2013-09-15 16:10:33.550 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.550 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
email = "duan@abccompany.ae";
firstname = "\n ";
lastname = "\n ";
memberId = "\n ";
})
2013-09-15 16:10:33.556 ECP[24094:c07] FOUND
2013-09-15 16:10:33.557 ECP[24094:c07] NOT INVALID
2013-09-15 16:10:33.557 ECP[24094:c07] DATA is <MemberDetails: 0x9533320> (entity: MemberDetails; id: 0x9533360 <x-coredata:///MemberDetails/tD0F9345D-847F-4E6A-9C2F-22F4A23CB1B02> ; data: {
email = "\n";
firstname = "\n ";
lastname = "\n ";
memberId = "\n ";
})
为什么它在保存数据和打印对象时为零? 什么是\ n被保存,它根本不是xml的一部分?
感谢
答案 0 :(得分:1)
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
此方法可能无法获得整个值 - 可以使用内容的位数多次调用它。因此,您应该在班级中使用NSMutableString
作为属性,并在每次调用时将string
附加到其中。然后在parser:didEndElement:namespaceURI:qualifiedName:
中使用它(然后清空字符串)。
同样,不要保存tagName
并尝试使用parser:foundCharacters:
,使用传入的元素名称和您的字符在parser:didEndElement:namespaceURI:qualifiedName:
中完成大部分工作找到并添加到你的可变字符串中。
您的日志显示您始终拥有相同的实例<MemberDetails: 0x9533320>
,并且您添加到其中的信息不断被覆盖或仅仅是垃圾。我怀疑这与您正在使用的标签有关,但您没有显示示例XML,因此很难判断。很可能还有一个事实,那就是你的大部分处理都在parser:foundCharacters:
方法中。