我正在使用NSXML解析器进行解析,但它没有调用解析器委托方法,它显示零错误但我不知道我在哪里犯了错误。我也需要解析属性内容。
提前致谢
这是我的xml文件和代码
** 1。
* * <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。
**
@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 :(得分:2)
在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 :(得分:0)
请检查xml中您从服务器端获取的编码方案作为回复,并检查您的iPhone客户端代码UTF8
,UTF16
或其他内容。