为什么UserFullname无法通过xml解析器检索?

时间:2011-11-04 09:05:58

标签: c# ios web-services xml-parsing xml-namespaces

这是Web服务方法和检索到的信息...... method SOAP 1.1

method HTTP POST

data retrieved

为什么无法检索UserFullname中的字符串?

xcode中的xmlparsing:

        //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的数据字符串...我的代码中有什么错误吗?谢谢你的帮助...

2 个答案:

答案 0 :(得分:0)

您是否尝试使用UserFullName的单个令牌。 (例如“汤姆”)

是否可能与未正确编码有关?

答案 1 :(得分:0)

好的解决了...在ftp服务器中我不仅需要添加webservice .asmx文件,而且在ftp http docs文件夹中我必须将webservice .dll和.pbd文件放入bin文件夹...