0

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

    -(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];
 }  
 }  

谢谢你帮助我

4

2 回答 2

0

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

于 2013-01-03T09:34:31.770 回答
0

您是否已将NSXMLParserDelegate和包含ASIHTTPRequestDelegate在您的.h文件中?

您正在调用该方法来解析来自ASIHTTPRequest Check the flow of Check the flow using 的委托的 XML breakpoints

@interface yourClassName: NSObject <NSXMLParserDelegate, ASIHTTPRequestDelegate>

还要检查这些 stackoverflow 问题

于 2013-01-03T06:32:38.663 回答