首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSXML XML解析问题iOS

NSXML XML解析问题iOS
EN

Stack Overflow用户
提问于 2013-11-14 19:11:41
回答 1查看 150关注 0票数 0

我知道有很多问题已经被问到和回答了,但没有一个是关于我的问题的。

我正在将XML发布到服务器上,并且得到了一个响应。我的问题是从响应中取回一个指定的键。

我正在尝试列出所有的性别,例如男性,女性和他们的ID,然而,当使用NSXML将XML解析为文本时,我只得到女性和ID 2,而我没有得到男性?

我已经研究并尝试解决这个问题,但无济于事。

下面是我的代码:

代码语言:javascript
复制
- (void)getGender {

    NSString *soapMessage = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                             "<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/\" xmlns:app=\"http://app.ws.api.bells.wigroup.com/\">"
                             "<soap:Header/>\n"
                             "<soap:Body>\n"
                             "<app:getAllGenders>\n"
                             "<request>\n"
                             "<apiCredentials>\n"
                             "<apiId>IPHONE_APP</apiId>\n"
                             "<sha1Password>8656cafcd71cbbfe773a0fdb6c422666a80e5b8f</sha1Password>\n"
                             "</apiCredentials>\n"
                             "<request>\n"
                             "</request>\n"
                             "</request>\n"
                             "</app:getAllGenders>\n"
                             "</soap:Body>\n"
                             "</soap:Envelope>\n"
                             ];
    NSLog(@"Message%@",soapMessage);

    NSURL *url = [NSURL URLWithString:@"http://qa.wigroup.co:8080/bells/AppWS"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    //[theRequest addValue: @"http://" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {

        NSURLResponse *response;
        NSError *error;

        NSData *urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
        NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
        NSLog(@"Login response XML:%@",str);


        // create and init NSXMLParser object
        XmlArrayParser *parser = [[XmlArrayParser alloc] initWithData:urlData];
        parser.rowElementName = @"return";
        parser.elementNames = [NSArray arrayWithObjects:@"response", @"responseCode", @"responseDesc", @"gendersList", nil];
        parser.attributeNames = [NSArray arrayWithObjects:@"id", @"gender", nil];

        if ([parser.rowElementName isEqualToString:@"responseCode"] && _flag)
        {
            //read the value here
             NSLog(@"flagging");
        }

        // parsing...
        BOOL success = [parser parse];

        // test the result
        if (success)
        {
            NSMutableArray *loginAuth = [parser items];

            //        self.textView.text = [NSString stringWithFormat:
            //        @"This is an array of dictionaries, one dictionary for each user:\n\n%@",
            //        [users description]];

           // NSDictionary *eventLocation = [NSDictionary dictionaryWithObjectsAndKeys:@"response", nil];

            NSDictionary *loginResponse = [loginAuth objectAtIndex:0];         // this retrieves the first user
            NSString *userResponse = loginResponse[@"gendersList"];           // this retrieves that user's username in Xcode 4.5 and greater

            NSString *userRes = [loginResponse objectForKey:@"id"];

            NSString *test = [loginAuth description];

            NSLog(@"Returned Code loginResponse %@",loginResponse);
            NSLog(@"Returned Code userResponse %@ %@",userResponse, userRes);
            NSLog(@"Returned Code test %@",test);

            NSMutableArray *array=[[NSMutableArray alloc]initWithCapacity:10];

            for (NSDictionary *defineXMLData in loginAuth) {

                NSNumber * responseCode = [defineXMLData objectForKey:@"responseCode"];
                NSArray * responseDEsc = [defineXMLData objectForKey:@"responseDesc"];
                NSArray * genderList = [defineXMLData objectForKey:@"gendersList"];
               // NSArray * gender = [defineJsonData objectForKey:@"gender"];

                NSLog(@"Genders%@", genderList);

                [array addObject: responseCode];
                [array addObject: responseDEsc];
                [array addObject: genderList];
                //[array addObject: gender];
                // [array addObject: vouchersUser];
            }

            label.numberOfLines = 2000; // for example
            label.lineBreakMode = NSLineBreakByClipping;
            NSString *output=[array componentsJoinedByString:@","];
            label.text = [NSString stringWithFormat:@"XML Result: %@ ",output];

            [SVProgressHUD dismiss];

            //            [[[UIAlertView alloc] initWithTitle:nil
            //                                        message:[NSString stringWithFormat:@"No errors - user count : %i", [[parser items] count]]
            //                                       delegate:nil
            //                              cancelButtonTitle:@"OK"
            //                              otherButtonTitles:nil] show];

        }
        else
        {
            NSLog(@"Error parsing document!");

            //            [[[UIAlertView alloc] initWithTitle:nil
            //                                        message:@"Error parsing document!"
            //                                       delegate:nil
            //                              cancelButtonTitle:@"OK"
            //                              otherButtonTitles:nil] show];
        }

    }
    else
    {
        NSLog(@"theConnection is NULL");
    }

    NSLog(@"test");

}

和我从nslog中得到的XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getAllGendersResponse xmlns:ns2="http://app.ws.api.bells.wigroup.com/">
         <return>
            <responseCode>-1</responseCode>
            <responseDesc>Success</responseDesc>
            <response>
               <responseCode>-1</responseCode>
               <responseDesc>Success</responseDesc>
               <gendersList>
                  <gender>Male</gender>
                  <id>1</id>
               </gendersList>
               <gendersList>
                  <gender>Female</gender>
                  <id>2</id>
               </gendersList>
            </response>

只是在解析:

代码语言:javascript
复制
{
gendersList = Female2;
responseCode = "-1";
responseDesc = Success;
}
EN

回答 1

Stack Overflow用户

发布于 2013-11-14 19:20:13

您没有正确指出问题所在。您的代码将解析整个文档,但只保留最后一项(last genderList、last responseCode、last responseDesc)。初始化一个数组,并在解析时在for循环中将每个字典添加到数组中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19976155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档