Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to parse some xml data ,but its showing parse failed.below is the code
VB
 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:@"&lt"
                                         withString:@"<"];
    theXML = [theXML stringByReplacingOccurrencesOfString:@"&gt"
                                               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>&lt;reminders&gt;  &lt;medication&gt;    &lt;name&gt;Crocin&lt;/name&gt;    &lt;medicationid&gt;822&lt;/medicationid&gt;    &lt;alerttime&gt;15:50,08:00,08:18&lt;/alerttime&gt;    &lt;dow&gt;Sun,Mon,Tue,Wed,Thu,Fri,Sat&lt;/dow&gt;    &lt;unit&gt;&lt;/unit&gt;    &lt;description&gt;&lt;/description&gt;  &lt;/medication&gt;  &lt;medication&gt;    &lt;name&gt;test&lt;/name&gt;    &lt;medicationid&gt;831&lt;/medicationid&gt;    &lt;alerttime&gt;08:00,12:00,18:00&lt;/alerttime&gt;    &lt;dow&gt;Sun,Fri&lt;/dow&gt;    &lt;unit&gt;&lt;/unit&gt;    &lt;description&gt;&lt;/description&gt;  &lt;/medication&gt;&lt;/reminders&gt;</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
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900