解析方法未在NSXML解析器中调用

解析方法未在NSXML解析器中调用

问题描述:

我正在使用NSXML解析器进行解析,但它没有调用解析器委托方法,它显示零错误,但我不知道我在哪里犯了一个错误。我也需要解析属性内容。解析方法未在NSXML解析器中调用

在此先感谢

这里是我的xml文件和代码

** 1。

  • xml文件

* * <ROOT_ELEMENT><RESPONSE READ_TAG="LEVEL_LIST" RESULT="" TEXT=""/><USER USER_NAME="newadmin01" TOKEN_ID="0.6864221651800831" FULL_NAME="newadmin01, newadmin01"/><DATETIME UNFORMATTED_TEXT="Aug 10 2011 5:23PM" FORMATTED_TEXT="10 Aug 17:23"/><BREADCRUMB/><LEVEL_LIST><LEVEL ID="4519" NAME="Mega Mart" CHILD_EXISTS="Y" ADD_EDIT_PRIVILEGE="Y"/></LEVEL_LIST></ROOT_ELEMENT>

** 2。

  • h文件

**

@interface MainLevelList : UIViewController 
<UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate, UISearchBarDelegate, NSXMLParserDelegate> { 
    UITableView   *theTableView; 
    UILabel    *lbl_title; 
    UILabel    *lbl_time; 
    NSMutableArray  *mainLevelListArray;  

    NSXMLParser   *parser; 
    NSMutableString  *elemName; 
    NSMutableString  *currentValueString; 
} 

@property (nonatomic,retain) IBOutlet UILabel   *lbl_title; 
@property (nonatomic,retain) IBOutlet UILabel   *lbl_time; 
@property (nonatomic,retain) IBOutlet UITableView  *theTableView; 
@property (nonatomic, retain)   NSMutableArray *mainLevelListArray; 



- (IBAction)onClickLeftArrow; 
- (IBAction)onClickRightArrow; 

@end 

** 

- **3. .m file 

** 

@implementation MainLevelList 

    @synthesize mainLevelListArray; 
    @synthesize theTableView; 
    @synthesize lbl_title; 
    @synthesize lbl_time; 



    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
     if (self) { 
      // Custom initialization. 
      UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [button1 setImage:[UIImage imageNamed:@"answerBtn.png"] forState:UIControlStateNormal]; 
      [button1 setFrame:CGRectMake(0, 0, 66, 34)]; 
      [button1 addTarget:self action:@selector(answerBtn:) forControlEvents:UIControlEventTouchUpInside]; 
      [button1 setTitle:@"Answer" forState:UIControlStateNormal]; 

      UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button1]; 
      NSMutableArray *toolBarItems = [[[NSMutableArray alloc] init] autorelease]; 
      [toolBarItems addObject:barButtonItem]; 
      //[self setToolbarItems:toolBarItems]; 

      UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [button3 setImage:[UIImage imageNamed:@"btmbar_Bg.png"] forState:UIControlStateNormal]; 
      [button3 setFrame:CGRectMake(150, 0, 66, 34)]; 
      [button3 addTarget:self action:@selector(home:) forControlEvents:UIControlEventTouchUpInside]; 
      [button3 setTitle:@"Home" forState:UIControlStateNormal]; 

      UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button3]; 
      [toolBarItems addObject:barButtonItem2]; 
      button3.hidden = YES; 
      //[self setToolbarItems:toolBarItems]; 

      UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [button4 setImage:[UIImage imageNamed:@"btmbar_Bg.png"] forState:UIControlStateNormal]; 
      [button4 setFrame:CGRectMake(200, 0, 66, 34)]; 
      [button4 addTarget:self action:@selector(home:) forControlEvents:UIControlEventTouchUpInside]; 
      [button4 setTitle:@"Home" forState:UIControlStateNormal]; 

      UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc] initWithCustomView:button4]; 
      [toolBarItems addObject:barButtonItem3]; 
      button4.hidden = YES; 

      UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [button2 setImage:[UIImage imageNamed:@"nextBtn.png"] forState:UIControlStateNormal]; 
      [button2 setFrame:CGRectMake(250, 0, 66, 34)]; 
      [button2 addTarget:self action:@selector(home:) forControlEvents:UIControlEventTouchUpInside]; 
      [button2 setTitle:@"Next" forState:UIControlStateNormal]; 

      UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:button2]; 
      [toolBarItems addObject:barButtonItem1]; 
      [self setToolbarItems:toolBarItems]; 



     } 
     return self; 
    } 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 


     NSString *responseXml = [[NSString alloc] initWithString:[[NSUserDefaults standardUserDefaults] valueForKey:@"responseXml"]]; 
     //[[NSUserDefaults standardUserDefaults] setObject:responseXml forKey:@"responseXml"]; 
     parser = [[NSXMLParser alloc] initWithData:[responseXml dataUsingEncoding:NSUTF8StringEncoding]]; 
     [responseXml release]; 

     [parser setDelegate:self]; 
     BOOL parseFlag = [parser parse]; 
     if (parseFlag == 1) 
      if(debug)NSLog(@"parseFlag = YES"); 
      else 
      if(debug)NSLog(@"parseFlag = NO"); 



     UINavigationBar *bar = [self.navigationController navigationBar]; 

     CGRect labelRect = CGRectMake(255, 12, 60, 18); 
     UILabel *label = [[UILabel alloc] initWithFrame:labelRect]; 
     [bar addSubview:label]; 
     label.text = @"ALRICK"; 
     label.textAlignment = UITextAlignmentLeft; 
     label.font = [UIFont boldSystemFontOfSize:14]; 
     label.tag = 10; 
     [label release]; 

     UIButton *logOutButton = [UIButton buttonWithType:100]; 
     [logOutButton setImage:[UIImage imageNamed:@"exitBtn.png"] forState:UIControlStateNormal]; 
     [logOutButton addTarget:self action:@selector(logoutButtonTouched) forControlEvents:UIControlEventTouchUpInside]; 
     [logOutButton setTitle:@"Logout" forState:UIControlStateNormal]; 
     UIBarButtonItem* logOutItem = [[UIBarButtonItem alloc] initWithCustomView:logOutButton]; 
     self.navigationItem.leftBarButtonItem = logOutItem; 

     [lbl_title setText:@"Chain"]; 
     [lbl_title setTextColor:[UIColor whiteColor]]; 

     NSString *date = [NSDate date]; 
     //NSString *date = [[NSDate date] initWithFormat:@"EEE, MMM, h:mm"]; 
     NSLog(@"date:%@",date); 
     [lbl_time setText:[NSString stringWithFormat:@"%@",date]]; 
     [lbl_time setTextColor:[UIColor whiteColor]]; 

     mainLevelListArray = [[NSMutableArray alloc] init]; 
     //[tableData addObject:@"ICA"]; 
    // [tableData addObject:@"Bread"]; 
    // [tableData addObject:@"ISF"]; 
    // [tableData addObject:@"OSF"]; 
    // [tableData addObject:@"BBC"]; 
    // [tableData addObject:@"ACC"]; 
    // [tableData addObject:@"CFF"]; 
    } 

    -(void)answerBtn:(id)sender { 
     QuestionnaireListView *m_questionnaireList = [[QuestionnaireListView alloc] initWithNibName:@"QuestionnaireListView" bundle:nil]; 
     [self.navigationController pushViewController:m_questionnaireList animated:YES]; 
     [m_questionnaireList release]; 
    } 

    - (void)logoutButtonTouched { 
     [self.navigationController popViewControllerAnimated:YES]; 
    } 

    - (IBAction)onClickLeftArrow { 

     //[self.navigationController popViewControllerAnimated:YES]; 
    } 

    - (IBAction)onClickRightArrow { 

     //[self.navigationController popViewControllerAnimated:YES]; 
    } 


    - (void)viewDidUnload { 
     [super viewDidUnload]; 
    } 

    - (void)dealloc { 
     [super dealloc]; 
     [mainLevelListArray release]; 
    } 

    #pragma mark - UITableView delegate methods 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     return 1; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     return [mainLevelListArray count]; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     static NSString *kCustomCellID = @"MyCellID"; 

     UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease]; 
     } 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
      cell.textLabel.text = [mainLevelListArray objectAtIndex:indexPath.row]; 

      UIView *m_view = [[[UIView alloc] init] autorelease]; 
      m_view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listHighlight_bg.png"]]; 
      cell.selectedBackgroundView = m_view; 
      return cell; 
    } 


    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 
     NSLog(@"element Name = %@", elementName); 
     NSLog(@"namespace URI = %@", namespaceURI); 
     NSLog(@"qualified Name = %@", qName); 
     NSLog(@"attributeDict = %@", attributeDict); 
     elemName = [[NSString alloc] initWithString:elementName]; 
     attributeDict = [[NSString alloc] initWithString:[attributeDict objectForKey:@"READ_TAG"]]; 
     //attribute = [attributeDict objectForKey:@"READ_TAG"]; 

    } 

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
     if ([elemName isEqualToString:@"RESPONSE"]) { 
      if (!currentValueString) { 
       currentValueString = [[NSMutableString alloc] initWithCapacity:1024]; 
      } 
      [currentValueString appendString:string]; 
     } 
     else if ([elemName isEqualToString:@"USER"]) { 
      if (!currentValueString) { 
       currentValueString = [[NSMutableString alloc] initWithCapacity:1024]; 
      } 
      [currentValueString appendString:string]; 
     } 
     if ([elemName isEqualToString:@"DATETIME"]) { 
      if (!currentValueString) { 
       currentValueString = [[NSMutableString alloc] initWithCapacity:1024]; 
      } 
      [currentValueString appendString:string]; 
     } 
     else if ([elemName isEqualToString:@"BREADCRUMB"]) { 
      if (!currentValueString) { 
       currentValueString = [[NSMutableString alloc] initWithCapacity:1024]; 
      } 
      [currentValueString appendString:string]; 
     } 
     else if ([elemName isEqualToString:@"LEVEL"]) { 
      if (!currentValueString) { 
       currentValueString = [[NSMutableString alloc] initWithCapacity:1024]; 
      } 
      [currentValueString appendString:string]; 
     } 

    } 

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 
     if ([elemName isEqualToString:@"RESPONSE"]) { 
      [mainLevelListArray addObject:currentValueString]; 

      [currentValueString release]; 
      currentValueString = nil; 

      [elemName release]; 
      elemName = nil; 

     } 
     else if ([elemName isEqualToString:@"USER"]) { 
      [mainLevelListArray addObject:currentValueString]; 

      [currentValueString release]; 
      currentValueString = nil; 

      [elemName release]; 
      elemName = nil; 

     } 
     else if ([elemName isEqualToString:@"DATETIME"]) { 
      [mainLevelListArray addObject:currentValueString]; 

      [currentValueString release]; 
      currentValueString = nil; 

      [elemName release]; 
      elemName = nil; 

     } 
     else if ([elemName isEqualToString:@"BREADCRUMB"]) { 
      [mainLevelListArray addObject:currentValueString]; 

      [currentValueString release]; 
      currentValueString = nil; 

      [elemName release]; 
      elemName = nil; 

     } 
     else if ([elemName isEqualToString:@"LEVEL"]) { 
      [mainLevelListArray addObject:currentValueString]; 

      [currentValueString release]; 
      currentValueString = nil; 

      [elemName release]; 
      elemName = nil; 
     } 

    } 






    @end 




** 
+0

你也可以粘贴你的日志吗? –

加入viewDidLoad方法几行下面

  • (无效)viewDidLoad中{ 给出[超级viewDidLoad中];

// NSMutableString * urlString; //指定URL字符串

NSString *postLength = [NSString stringWithFormat:@"%d", [urlString length]]; 

NSURL *url = [NSURL URLWithString:urlString]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:[urlString dataUsingEncoding:NSUTF8StringEncoding]]; 

NSError *error; 
NSURLResponse *response; 
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

//写的viewDidLoad }您的剩余部分

+1

将您的完整xml粘贴到您想要解析的标题中。 – 2011-08-11 08:06:28

请检查XML编码方案,你从服务器端获取作为响应,并检查您的iPhone客户端代码,这是UTF8UTF16或其他东西。