Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2012, 10:54 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 12
idny99 is on a distinguished road
Default XML is not being parsed correctly (NSData)

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.

Last edited by idny99; 02-09-2012 at 10:57 AM.
idny99 is offline   Reply With Quote
Old 02-09-2012, 11:54 AM   #2 (permalink)
Use [code] tags please
 
Join Date: Jun 2009
Location: Jacksonville, FL
Posts: 410
timle8n1 is on a distinguished road
Default

Quote:
Originally Posted by idny99 View Post
Code:
after NSData ----->: <3c517565 7374696f 6e6e6169 7265584d 4c20786d 6c6e733d 22687474 703a2f2f 73636865 6d61732e 64617461 636f6e74 72616374 2e6f7267
How would you expect NSData to appear?

Quote:
Originally Posted by idny99 View Post
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.
I think you assume wrong the fact that your log statement appears tells you that the NSXMLParser found an element named QuestionnaireName.

The problem is QuestionnaireName is not an attribute within QuestionnaireName. An attribute takes the form

Code:
<Element Attribute="foo">ElementData</Element>
To use NSXMLParser to capture the ElementData you need to create something to accumulate the data when didStartElement: is called, implement foundCharacters: to append to that something, and implement didEndElement: to know you call all the characters in the ElementData.

See here - Parsing XML Files - Xcode Tutorials
timle8n1 is offline   Reply With Quote
Reply

Bookmarks

Tags
url, web service, xml, xmlparser

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 390
11 members and 379 guests
chiataytuday, chits12345, fiftysixty, gmarro, KennyChong, kilobytedump, Leslie80, Matrix23, Meoz, ryantcb, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,670
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Yosh_K
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:25 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0