【发布时间】:2011-12-21 20:36:32
【问题描述】:
这是网络服务方法和检索到的信息...
为什么无法检索到UserFullname中的字符串?
xcode 中的xml解析:
//connect to webservice
NSString *soapMsg = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<RetrieveUser xmlns=\"http://dev.cel.nie.edu.sg/\">"
"<password>%@</password>"
"</RetrieveUser>"
"</soap:Body>"
"</soap:Envelope>", passWordField.text];
//requesting
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://dev.cel.nie.edu.sg/projects/VideoApp/VideoWebService.asmx"]];
//message length??
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
//building the post xml
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://dev.cel.nie.edu.sg/RetrieveUser" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
//connection
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
//[req release];
if(conn){
webData = [[NSMutableData data] retain];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[conn release];
[webData release];
NSString * errorString = [NSString stringWithFormat:@"Please connect to the internet to continue."];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
xmlParser = [[NSXMLParser alloc] initWithData:webData];
[xmlParser setDelegate:self];
[xmlParser setShouldProcessNamespaces:NO];
[xmlParser setShouldReportNamespacePrefixes:NO];
[xmlParser setShouldResolveExternalEntities:NO];
[xmlParser parse];
[conn release];
[webData release];
}
-(void)parserDidStartDocument:(NSXMLParser *)parser{
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//retrieve lecturer's ID, name and photo according to the password. Retrieve from web service
currentElement = [elementName copy];
if ([elementName isEqualToString:@"UserID"]) {
// clear out our story item caches...
currentID = [[NSMutableString alloc] init];
currentName = [[NSMutableString alloc] init];
lectFullName = [[NSMutableString alloc] init];
currentImg = [[NSMutableString alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// save the characters for the current item...
if ([currentElement isEqualToString:@"UserID"]) {
[currentID appendString:string];
}else if([currentElement isEqualToString:@"Username"]){
[currentName appendString:string];
NSLog(@"lect username: %@", currentName);
}else if([currentElement isEqualToString:@"UserFullname"]){
[lectFullName appendString:string];
NSLog(@"lectname: %@", lectFullName);
}else if([currentElement isEqualToString:@"UserImage"]){
[currentImg appendString:string];
NSLog(@"lect img: %@", currentImg);
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
}
输出:
2011-11-04 16:45:16.314 MyLauncher[4538:707] lect username: tom
2011-11-04 16:45:16.317 MyLauncher[4538:707] lect img: http://dev.cel.nie.edu.sg/projects/VideoApp/LecturerImages/ID2--DaPe--2011-11-03-Ti7e--04-07-38.jpg
我只是不明白它没有检索 UserFullname 的数据字符串...我的代码中有错误吗?谢谢你的帮助......
【问题讨论】:
-
在 else if 循环中修复断点并检查控件是否到达那里。
-
好的,我发现我更新了 asmx 文件并将其放入我的 ftp 服务器.. 但没有读取更新后的文件.. 它仍然只识别旧文件...嗯
标签: c# ios web-services xml-parsing xml-namespaces