Hello,
I am trying to get xml from an .NET web service which I will then parse through an instance of NSXML.
If you wish to test the web service output (xml) please use
http://www.osqar.co.uk/OsqarWCF/fran...reForID?id=396
Below is my URL request to my web service. NSLog shows the xml was read successfully. This is shown in the line NSLog(@"Response: %@",responseString);. This string is then given to the XMLParser.
Code:
// viewController.m
-(IBAction)getQuestionnaire
{
// Obtain questionnaire ID from input textfield
NSString *qid = questionnaireId.text;
// Begin creating URL string and contact web service
dataWebService = [NSMutableData data];
NSString *combinedRequest = [NSString stringWithFormat:@"http://www.osqar.co.uk/OsqarWCF/frank.svc/getQuestionnaireForID?id=%@", qid];
NSURL * myURL = [NSURL URLWithString:combinedRequest];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: myURL];
NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[myConnection start];
outputLabel.text = qid;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[dataWebService setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[dataWebService appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@",responseString);
// NSLog show that XML was successfully read and it displayed in debug output
XMLParser *parseQuestionnaire = [[XMLParser alloc] init];
Questionnaire *myQuestionnaire = [parseQuestionnaire parseXML:responseString];
}
Now when I try and parse the XML from the viewController to the XMLParser class the data appears to get scrambled. (See XMLParser.m below). The first NSLog statement "Incoming xml ------>" shown in the debug output that the xml is correct. However after the NSData command the NSLog statement outputs like this.
Code:
after NSData ----->: <3c517565 7374696f 6e6e6169 7265584d 4c20786d 6c6e733d 22687474 703a2f2f 73636865 6d61732e 64617461 636f6e74 72616374 2e6f7267
After this the XML parser appears to do nothing.
Code for XML Parse below:
Code:
#import "XMLParser.h"
#import "Questionnaire.h"
@implementation XMLParser
-(Questionnaire *)parseXML:(NSString *)xml
{
userRet = [[Questionnaire alloc] init];
NSLog(@"incoming xml ----->: %@",xml);
NSData *data = [xml dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"after NSData ----->: %@",data);
// data appears to be in hexidecimal
BOOL success;
questionnaireParser = [[NSXMLParser alloc] initWithData:data];
[questionnaireParser setDelegate:self];
[questionnaireParser setShouldResolveExternalEntities:YES];
success = [questionnaireParser parse];
return userRet;
}
- (void) parser: (NSXMLParser *) parser didStartElement: (NSString *) elementName
namespaceURI: (NSString *) namespaceURI
qualifiedName: (NSString *) qName
attributes: (NSDictionary *) attributeDict
{
if ([elementName isEqualToString:@"QuestionnaireName"])
{
NSString *name = [attributeDict objectForKey:@"QuestionnaireName"];
NSLog(@"QuestionnaireNameBefore: %@", name);
}
}
If I let the code continue then the NSLog statement above (@"QuestionnaireNameBefore: %@, name) returns (null). From this I assume the XML parse cannot read the data being input because of the hexidecimal form it appears in.
How can I parse this XML correctly? How can I get name element to be identified? What is causing the NSData command to scramble the XML?
I hope this made sense. Thanks in advance.