I am trying to parse XML from a .NET WCF Webservice using NSXML Parser.
Here is the XML which my web-service returns:
<QuestionnaireXML xmlns="http://schemas.datacontract.org/2004/07/QuestionnaireDescriptor" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<QuestionnaireName>Clinic Waiting Room Feedback</QuestionnaireName>
<Questions>
<Question>
<Answers>
<Answer>
<AnswerTitle>Windows</AnswerTitle>
</Answer>
<Answer>
<AnswerTitle>Mac</AnswerTitle>
</Answer>
</Answers>
<QuestionID>363</QuestionID>
<QuestionName>Windows or Mac?</QuestionName>
</Question>
</Questions>
</QuestionnaireXML>
I obtain the XML using the following method:
-(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];
}
- (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];
XMLParser *parseQuestionnaire = [[XMLParser alloc] init];
Questionnaire *myQuestionnaire = [parseQuestionnaire parseXML:responseString];
}
And then parse the XML to the XMLParser class and it method. Like So:
#import "XMLParser.h"
#import "Questionnaire.h"
@implementation XMLParser
@synthesize xmlParser;
-(Questionnaire *)parseXML:(NSString *)xml
{
userRet = [[Questionnaire alloc] init];
NSData *data = [xml dataUsingEncoding:NSUTF8StringEncoding];
self.xmlParser = [[NSXMLParser alloc] initWithData:data];
self.xmlParser.delegate = self;
if([self.xmlParser parse])
{
NSLog(@"The Xml is parsed.");
}
else
{
NSLog(@"Failed to parse the XML");
}
return userRet;
}
- (void) parser: (NSXMLParser *) parser parseErrorOccurred: (NSError *) parseError
{
NSLog(@"Error Parser:%@",[parseError localizedDescription]);
}
- (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(@"QuestionnaireName: %@", name);
userRet.QuestionnaireName = [attributeDict objectForKey:@"QuestionnaireName"];
}
}
My problem here is that the element name QuestionnaireName is apparently not being found. I have placed a break point and the code steps into didStartElement method but then steps out of the if ([elementName isEqualToString:@”QuestionnaireName”]) statement.
As you can see from the XML sample above, the Element QuestionnaireName clearly exists.
What am I doing wrong here? Have I missed out anything?
Any help would be greatly appreciated.
First you have to set
currentelement = elementNamein the method didstartelement.Now have to implement the method foundcharacter.
In that method check the following:
then you will get value “Clinic Waiting Room Feedback”