0

我正在解析这样的数据。

    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 的一部分?

谢谢

4

1 回答 1

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:方法中这一事实有关。

于 2013-09-26T19:33:17.387 回答