我正在使用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];
}
}