解析iOS中的XML响应



我正在创建一个iOS应用程序。在其中我必须使用SOAP网络服务来获取一些详细信息。这样我已经使用sudz-c生成了存根。我能够致电网络服务并得到回应。但是我无法解析回应。以下是XML响应。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <ViewAppTrackResponse xmlns="http://service.cmp.app.com">
         <ViewAppTrackResponseReturn>
            <ns1:monthBO xmlns:ns1="http://response.cmp.app.com">
               <monthListItem>
                  <ns2:date xmlns:ns2="http://bean.cmp.app.com">1-2-2014, Saturday (nonworking day)</ns2:date>
                  <ns3:lockStatus xmlns:ns3="http://bean.cmp.app.com">N</ns3:lockStatus>
                  <ns4:dailyTime xsi:nil="true" xmlns:ns4="http://bean.cmp.app.com"/>
                  <ns5:taskListNew xsi:nil="true" xmlns:ns5="http://bean.cmp.app.com"/>
               </monthListItem>
               <monthListItem>
                  <ns6:date xmlns:ns6="http://bean.cmp.app.com">2-2-2014, Sunday (nonworking day)</ns6:date>
                  <ns7:lockStatus xmlns:ns7="http://bean.cmp.app.com">N</ns7:lockStatus>
                  <ns8:dailyTime xmlns:ns8="http://bean.cmp.app.com">04:00</ns8:dailyTime>
                  <ns9:taskListNew xmlns:ns9="http://bean.cmp.app.com">
                     <taskListItem>
                        <ns9:trackId>1070</ns9:trackId>
                        <ns9:taskId>14</ns9:taskId>
                     </taskListItem>
                     <taskListItem>
                        <ns9:trackId>1094</ns9:trackId>
                        <ns9:taskId>44</ns9:taskId>
                     </taskListItem>
                  </ns9:taskListNew>
               </monthListItem>
               <monthListItem>
                  <ns10:date xmlns:ns10="http://bean.cmp.app.com">3-2-2014, Monday</ns10:date>
                  <ns11:lockStatus xmlns:ns11="http://bean.cmp.app.com">N</ns11:lockStatus>
                  <ns12:dailyTime xmlns:ns12="http://bean.cmp.app.com">08:00</ns12:dailyTime>
                  <ns13:taskListNew xmlns:ns13="http://bean.cmp.app.com">
                     <taskListItem>
                        <ns13:trackId>1071</ns13:trackId>
                        <ns13:taskId>14</ns13:taskId>
                     </taskListItem>
                     <taskListItem>
                        <ns13:trackId>1073</ns13:trackId>
                        <ns13:taskId>44</ns13:taskId>
                       </taskListItem>
                  </ns13:taskListNew>
               </monthListItem>
            </ns1:monthBO>
            <ns14:userId xsi:nil="true" xmlns:ns114="http://response.cmp.app.com"/>5</ns14:userId>
         </ViewAppTrackResponseReturn>
      </ViewAppTrackResponse>
   </soapenv:Body>
</soapenv:Envelope>

任何人都可以帮助我解析此回应。这对我有帮助。

libxml2包含在可可中。

http://www.raywenderlich.com/553/xml-tutorial-for-ios-how-to-to-choose-the-the-besst-est-parser-parser-for-your-iphone-project

http://www.cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-parsing-and-xpath.html

您可以使用NSXMLPARSER类来解析此。使用其委托方法可以解析。我正在尝试解析您的XML。它尚未完成。我为您提供了解析的基本代码。您必须做剩下的。 这里的" XMlinput"是XMLString类型的NSString。

    NSData* xmlData = [xmlInput dataUsingEncoding:NSUTF8StringEncoding];
    NSXMLParser  * xmlParser = [[NSXMLParser alloc] initWithData:[xmlData copy]];
    [xmlParser setDelegate:(id)self];
    [xmlParser setShouldResolveExternalEntities: YES];
    [xmlParser parse];

创建一个XMLPARSER对象并输入您的XMLDATA.SET委托。

     //this delegate calls when parsing start.Only once.
    - (void)parserDidStartDocument:(NSXMLParser *)parser
    {
           recordResults = NO;//declared in .h
           MonthFlag = NO;//declared in .h
           TaskFlag = NO;//declared in .h
           Arry = nil;//declared in .h
    }
   // This delgate calls when each tag name is found.
  -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName  namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
                attributes: (NSDictionary *)attributeDict
          {
            strElementName =  [elementName copy];//strElementName is declared in .h
                NSLog(@"%@",strElementName);
               if([elementName isEqualToString:@"monthListItem"]){
                  MonthFlag = YES;
               }
               if([elementName isEqualToString:@"taskListItem"]){
                 TaskFlag = YES;
               }
              strElementValue = @""; //strElementValue is declared in .h
          }

 //This is called when each tag value is found.
 -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
 {
        strElementValue = [NSString stringWithFormat:@"%@%@", strElementValue,[string copy]];
        NSLog(@"%@",strElementValue);
       //NSLog(@"%@ : %@",strElmentName,strElementValue);
       recordResults=(strElementValue.length > 0);
  }
  // This deleagte will call in the end of each tag name.
    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName
  {
           NSLog(@"%@ - %@",elementName,strElementValue);
           if (recordResults) {
             if (MonthFlag) {
                if(dicTemp==nil){
                   dicTemp = [[NSMutableDictionary alloc] init];
                for (int i=0; i<10; i++) {
                  [dicTemp setObject:@"" forKey:strElementName];
                }
              }
            [dicTemp setObject:strElementValue forKey:elementName ];
          }
             }     
              if(([elementName isEqualToString:@"monthListItem"] ) && dicTemp!=nil) {
                   if(Arry==nil)Arry = [[NSMutableArray alloc] init];
                       [Arry addObject:[dicTemp copy]];
                   dicTemp = nil;
                   [dicTemp release];
                   MonthFlag = NO;
                  NSLog(@"arry test %@",[Arry description]);
               }
      }
       // This delegate will call when parsing finishes . only once
       - (void)parserDidEndDocument:(NSXMLParser *)parser
       {
           recordResults = NO;
       }

最新更新