I want to parse some xml data ,but its showing parse failed.below is the code
This my post metod:-
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/\">\n"
"<soap:Header>\n"
"<AuthenticationHeader xmlns=\"Medtech/MMHWebservices/ExternalDeviceData\">\n"
"<Username>%@</Username>\n"
"<Password>%@</Password>\n"
"</AuthenticationHeader>\n"
"</soap:Header>\n"
"<soap:Body>\n"
"<GetAllAlerts xmlns=\"Medtech/MMHWebservices/ExternalDeviceData\" />\n"
"</soap:Body>\n"
"</soap:Envelope>\n",userName,passWord];
// NSUserDefaults *default2 = [NSUserDefaults standardUserDefaults];
// NSString*defaultUrl = [default2 valueForKey:@"url"];
// NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@ManageMyHealthWebService/ExternalApplicationService.asmx",defaultUrl]];
NSURL * url = [NSURL URLWithString:@"http://192.168.2.119/ManageMyHealthWebService/ExternalApplicationService.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"Medtech/MMHWebservices/ExternalDeviceData/GetAllAlerts" 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 )
{
webData = [NSMutableData data] ;
NSLog(@"Problem%@",webData);
}
else
{
NSLog(@"theConnection is NULL");
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
//NSString *theXML = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
theXML = [theXML stringByReplacingOccurrencesOfString:@"<"
withString:@"<"];
theXML = [theXML stringByReplacingOccurrencesOfString:@">"
withString:@">"];
theXML = [theXML stringByReplacingOccurrencesOfString:@";"
withString:@""];
theXML = [theXML stringByReplacingOccurrencesOfString:@";/"
withString:@"/"];
NSLog(@"response is.....%@",theXML);
// NSData *myData = [theXML dataUsingEncoding:NSUTF8StringEncoding];
//
// NSLog(@">>>>>>%@",myData);
self.xmlParser = [[NSXMLParser alloc] initWithData:webData];
[self.xmlParser setDelegate:self];
// [self.xmlParser setShouldProcessNamespaces:NO];
// [self.xmlParser setShouldReportNamespacePrefixes:NO];
// [self.xmlParser setShouldResolveExternalEntities:NO];
[self.xmlParser parse];
// Run the parser
[self.xmlParser parse];
if ([self.xmlParser parse])
{
NSLog(@"XML parsing was successful");
//dispatch_async(dispatch_get_main_queue(), ^{
//reload the data in the table view
[self.remindTable reloadData];
// });
}
else{
NSLog(@"XML parsing failed");
}
NSLog(@"Parse Error: %@", [self.xmlParser parserError]);
}
I am getting the response in below format
<pre lang="xml"><?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetAllAlertsResponse xmlns="Medtech/MMHWebservices/ExternalDeviceData">
<GetAllAlertsResult><reminders> <medication> <name>Crocin</name> <medicationid>822</medicationid> <alerttime>15:50,08:00,08:18</alerttime> <dow>Sun,Mon,Tue,Wed,Thu,Fri,Sat</dow> <unit></unit> <description></description> </medication> <medication> <name>test</name> <medicationid>831</medicationid> <alerttime>08:00,12:00,18:00</alerttime> <dow>Sun,Fri</dow> <unit></unit> <description></description> </medication></reminders></GetAllAlertsResult>
</GetAllAlertsResponse>
</soap:Body>
</soap:Envelope>
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
NSLog(@"Found a new element in the XML document");
if([elementName isEqualToString:@"reminders"]){
medicalList = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"medication"])
{
self.medic = [[Medications alloc] init];
}
NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentValue)
currentValue = [[NSMutableString alloc] initWithString:string];
else
[currentValue appendString:string];
NSLog(@"Processing Value: %@", currentValue);
}
//Sent by a parser object to its delegate when it encounters an end tag for a specific element
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSString *value=currentValue;
if (currentValue !=nil)
{
value=[value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
if([elementName isEqualToString:@"reminders"])
return;
else if([elementName isEqualToString:@"medication"])
{
[medicalList addObject:medic];
medic = nil;
}
else if([elementName isEqualToString:@"name"])
{
self.medic.name = value;
NSLog(@"current value....%@",value);
}
else if([elementName isEqualToString:@"alerttime"])
{
self.medic.alertTime = value;
NSLog(@"current value....%@",value);
}
else if([elementName isEqualToString:@"medicationid"])
{
self.medic.medId = [self.formatter numberFromString:value];
NSLog(@"current value....%@",value);
}
currentValue=nil;
Any kind of help will be appreciated. thank you