不调用NSXMLParserDelegate方法

时间:2012-12-06 07:54:43

标签: iphone xml ios5 nsxmlparser asiformdatarequest

嗨我解析一个xml文件,我得到了响应,我已经存储在responseString.My问题中,没有被调用的委托方法,这是我的解析代码

    -(void)getData
{
     NSURL *url = [NSURL URLWithString:@"http://quizpro.testshell.net/api/quiz/4"];
 NSData *data = [NSData dataWithContentsOfURL:url];  // Load XML data from web

 NSString *applicationDocumentsDir = 
 [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
 NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"quiz.xml"];

 NSLog(@"store path is %@",storePath);

 [data writeToFile:storePath atomically:TRUE];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

request.delegate=self;

[request startSynchronous];

NSError *error = [request error];

if (!error)
{
     NSData *responseData=[request responseData];

     NSString *data  =[[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];

     NSString *usableXmlString = [data stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

     NSLog(@"usableXmlString is %@",usableXmlString);

     NSData *usableData = [usableXmlString dataUsingEncoding:NSUTF8StringEncoding];

     NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:usableData];

    [xmlParser setDelegate:self];
    [xmlParser parse];

}   
}  

- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"requestFinished method");

// Use when fetching text

NSString *responseString = [request responseString];  

** I get the entire data here ** 
NSLog(@"responseString is %@",responseString);


NSData *xData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
//myCode.text = responseString;
//NSLog(@" response %@", responseString);

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xData];

[parser setDelegate:self];
[parser parse];
[parser release];
}

我编写了NSXMLParser委托方法,如下所示

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
 {
 NSLog(@"parser/didStartElement");

 currentTag = elementName;


 if ([currentTag isEqualToString:@"questions"])
 {
     exams_object=[[ExamsObject alloc]init];
     NSLog(@"%@",currentTag);
 } 

  if ([currentTag isEqualToString:@"Question"])
 {
  exams_object=[[ExamsObject alloc]init];
 }

 if ([currentTag isEqualToString:@"Response"])
 {
 exams_object.responseArray=[[NSMutableArray alloc]init];
 }
   NSLog(@"%@",currentTag);

 }  

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
 {
 NSLog(@"parser/didEndElement");

 if ([currentTag isEqualToString:@"questions"])
 {
     exams_object=[[ExamsObject alloc]init];
 } 

 if([elementName isEqualToString:@"Question"])
 {
 [mainArray addObject:exams_object];
 }

 }  

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)str
 {

 NSLog(@"parser/foundCharacters");

 if ([currentTag isEqualToString:@"questionText"])
 {
 [exams_object.questionArray addObject:str];
 } 
 if ([currentTag isEqualToString:@"responseText"])
 {
 [exams_object.responseArray addObject:str];
 }  
 }  

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

您是否在NSXMLParserDelegate文件中加入了ASIHTTPRequestDelegate.h

您正在调用方法来解析ASIHTTPRequest委托中的XML 使用breakpoints检查执行流程。

@interface yourClassName: NSObject <NSXMLParserDelegate, ASIHTTPRequestDelegate>

同时检查这些stackoverflow问题

答案 1 :(得分:0)

检查是否在@interface中添加了NSXMLParserDelegate,并在您正在使用的类中设置了parser.delegate = self。