2012-04-16 44 views
1

我正在使用iPhone中的Web服務並通過NSXMLParser解析它。我的服務返回對象數組,但是當我從Web服務獲取它並連接相同類型的值時。 Web服務生成以下響應。iPhone:如何通過網絡服務獲得數組值?

HTTP/1.1 200 OK 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 

<?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> 
    <GetClientTripsResponse xmlns="http://tempori.net/"> 
     <GetClientTripsResult> 
     <TripNumbers> 
      <ConfirmationsNumber>string</ConfirmationsNumber> 
      <TripState>string</TripState> 
     </TripNumbers> 
     <TripNumbers> 
      <ConfirmationsNumber>string</ConfirmationsNumber> 
      <TripState>string</TripState> 
     </TripNumbers> 
     </GetClientTripsResult> 
    </GetClientTripsResponse> 
    </soap:Body> 
</soap:Envelope> 

對於ConfirmationsNumber爲6位數字,TripState是字符串值NONE或歐維

但是,當我解析它給出

ConfirmationsNumber = 234589455623784523和

TripState = NONENONEONWAY

意味着它解析三個對象並連接它們。我想要隔離的值或至少逗號分隔。像234589,455623,784523或者什麼都應該是可拆分的。

下面是代碼

@interface TrackTripStatus : UITableViewController <NSXMLParserDelegate> { 
    NSMutableArray *tripList; 
    UILabel *noTripsLabel; 
    int indexCount; 
    BOOL elementFound; 

    // for Web service 
    NSMutableData *webData; 
    NSMutableString *soapResults; 
    NSURLConnection *conn; 
    NSXMLParser * xmlParser; 
    NSString * qelementName; 
    NSString * RequestStep; 
} 

- (void) getClientTrips; 
-(void) setControls; 
@end 

- (void) getClientTrips 
{ 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *CLIENTPHONENUMBER = [defaults stringForKey:@"CLIENTPHONENUMBER"]; 
    if(CLIENTPHONENUMBER == nil) 
     CLIENTPHONENUMBER = @""; 

    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>" 
         "<GetClientTrips xmlns=\"http://tempori.net/\">" 
         "<HomePhone>%@</HomePhone>" 
         "</GetClientTrips>" 
         "</soap:Body>" 
         "</soap:Envelope>",CLIENTPHONENUMBER]; 

    NSURL *url = [NSURL URLWithString: WebServiceURL]; 
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 

    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]]; 
    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [req addValue:@"http://Itcurves.net/GetClientTrips" forHTTPHeaderField:@"SOAPAction"]; 
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; //---set the HTTP method and body--- 
    [req setHTTPMethod:@"POST"]; 
    [req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSLog(@"%@\n", soapMsg); 
    RequestStep = @"GetClientTripsResult"; 
    if (conn) { 
     [conn release]; 
    } 

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; 

    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 { 
    NSLog(@"Test: %@", error); 
    [webData release]; 
    [connection release]; 
} 

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{ 
    NSLog(@".........DONE. Received Bytes: %d", [webData length]); 
    NSString *theXML = [[NSString alloc] //---shows the XML--- 
         initWithBytes:[webData mutableBytes] length:[webData length] 
         encoding:NSUTF8StringEncoding]; 
    NSLog(@"\n\n %@",theXML); 
    if([theXML length] < 120) 
    { 
     if(indexCount < 3) 
     { 
      if([RequestStep isEqualToString:@"GetClientTripsResult"]) 
       [self SendClientInfo]; 
      indexCount = indexCount +1; 
     } 
     else { 
      RequestStep = @"end"; 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result" message:@"Server was unable to process request" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
     } 

    } 

    [theXML release];                      


    /* CODE TO RELEASE XML start */ 
    if (xmlParser) 
    { 
     [xmlParser release]; 
    } 
    [soapResults setString:@""]; 

    xmlParser = [[NSXMLParser alloc] initWithData: webData]; 
    [xmlParser setDelegate:self]; 
    [xmlParser setShouldResolveExternalEntities:YES]; 

    /* CODE TO RELEASE XML end */ 
    NSLog(@"Step: %@, %@",RequestStep, soapResults); 
    if([RequestStep isEqualToString:@"GetClientTripsResult"]) 
    { 
     qelementName = @"ConfirmationsNumber"; 
     [xmlParser parse]; 
     if ([soapResults length]> 0) 
     { 
      NSString * ConformNo = soapResults; 
      qelementName = @"TripState"; 
      [soapResults setString:@""]; 
      [xmlParser parse]; 
      if ([soapResults length] > 1) { 
       NSLog(@"Step2: %@, %@",RequestStep, soapResults); 
       indexCount =0; 
       RequestStep = @"end"; 



      } 
      else{    
       qelementName = @"DeclineReason"; 
       [soapResults setString:@""]; 
       [xmlParser parse]; 
       RequestStep = @"end";    
      } 
     } 
    } 
    if([RequestStep isEqualToString: @"end"]) 
    { 
     // [connection release];                                  
     // [webData release];  
    } 
} 

回答

0

每當我使用NSXMLParser時,我都會使用回調來解析它。下面是我用這個代碼:

首先設置委託和類變量

@interface FooBarClass : UIViewController <NSXMLParserDelegate> 
{ 
    NSMutableString *currentElementValue; 
} 

那麼無論你需要得到的URL,並設置XMLParser的

NSString *address = [[NSString alloc] initWithString:@"http://somewebservice"]; 
NSURL *url = [[NSURL alloc] initWithString:address]; 

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 

[url release]; 

[xmlParser setDelegate:self]; 

[xmlParser parse]; 
[xmlParser release]; 

,然後回調:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
attributes:(NSDictionary *)attributeDict 
{ 

if([elementName isEqualToString:@"Foo"]) { //start of main element to save 
    if (Bar) 
    { 
     [Bar release]; 
     Bar = nil; 
    } 
    Bar = [BarType alloc]; 
} 

//finding characters in an element. could be multiple calls so append here 
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 

if(!currentElementValue) 
    currentElementValue = [[NSMutableString alloc] initWithString:string]; 
else 
    [currentElementValue appendString:string]; 
} 

//ending elements 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 

NSString *trimmedValue = [currentElementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

if([elementName isEqualToString:@"Foo"]) //this is the end of the top level object so add it to our array 
{ 
    [fooArray addObject:Bar]; 

} 
else if([elementName isEqualToString:@"Foo2"]) //this is one of the features so we set it in our object 
{ 
    Bar.fooBarString = trimmedValue; 
} 



[currentElementValue release]; //release this to make room for the next element 
currentElementValue = nil; 
} 
0

我與庫調用sudzc工作,並生成Web服務的所有方法,這樣你就不必解析服務器響應,並直接獲得陣列準備使用。