Special characters in a soap message

2,743 views
Skip to first unread message

Lamia Hannoun

unread,
Dec 20, 2010, 11:40:06 AM12/20/10
to iphonesdkd...@googlegroups.com
Hi,
I have an issue  getting a correct format of data that I receive from my webservice.For example, when I get Data: x&y and put it in an array I excpet to get x&y at the same index as a whole data but It's separated in 3 elements x then & the y. I looked my encoding and I get encoding UTF8.
 
Do you have any idea why it doens't work 

Till Toenshoff

unread,
Dec 20, 2010, 10:30:24 PM12/20/10
to iphonesdkd...@googlegroups.com

Tried & instead of plain & ?

 

Pure guessing – the parser might interpret that &-character as an HTTP-get-delimiter, hence you might want to specify  & instead for a test.

 

--
You received this message because you are subscribed to the Google Groups "iPhone SDK Development" group.
To post to this group, send email to iphonesdkd...@googlegroups.com.
To unsubscribe from this group, send email to iphonesdkdevelop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/iphonesdkdevelopment?hl=en.

Daniel Baktiar

unread,
Dec 20, 2010, 11:55:09 PM12/20/10
to iphonesdkd...@googlegroups.com
i believe '&' (ampersand) that you've used has a special meaning in xml. it is an xml escape character.  
if your web service is using xml, then your web service should return 'x&y' instead of 'x&y'.

---
daniel baktiar
http://savinggaia.tritiumapps.com - saving the planet is everyone's business!




--

Lamia Hannoun

unread,
Dec 21, 2010, 3:28:54 AM12/21/10
to iphonesdkd...@googlegroups.com

Hi !

Thanks for your reply, I checked the reponse of the web service and in the xml I have x&y actually but the xmlparser interpret it as a delimiter as you said and split the word on 3 parts, I have tha problem also with the other special character especially french ones(é,ô,',....) 

I'm really stack I can't control or manage tha data that I get from the webservice.

Here is my methods for parsing xml 

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName

   attributes: (NSDictionary *)attributeDict

{

NSLog(elementName);


if( [elementName isEqualToString:@"Result"])

{

if(!soapArrayResults)

{

soapArrayResults = [[NSMutableArray alloc] init];

}

xmlArrayResults = YES;

}

}


-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

if (xmlArrayResults) {

NSLog(string);

[soapArrayResults addObject:string];

}

}

Thanks
2010/12/21 Till Toenshoff <m...@mmsguru.de>

jrparro

unread,
Dec 21, 2010, 12:48:52 AM12/21/10
to iphonesdkd...@googlegroups.com
Hi,

I have this on my note but it would strip html tags..

Hope that helps

Cheers

Till Toenshoff

unread,
Dec 21, 2010, 4:21:50 PM12/21/10
to iphonesdkd...@googlegroups.com

If you really get those french characters from the web-server-feed, then it is pretty clear, they are not providing you with wellformed xml in the first place. Well, then again, it might be a customer/partner that you have no handle on, hence you will have to preparse that result you get from the server and make wellformed XML out of it before feeding it to an XML parser. That means, replace any special characters with proper XML-representations.

 

It will be a bunch of characters that you need to replace, looking something like the code below (note, this is an ancient C++ method  I once did – this is not objectiveC, hence possibly not usable out of the box for you unless you are using objectiveC++ already [.mm]).

It builds a straight std::map (hashmap) for the characters to replace and the replacements. This could e.g. be adapted to be done through an NSDictionary and possibly needs more characters to cover – however, you may get the idea…

 

#include <map>

 

/*\

* <---------- sXmlTidy ---------->

* @m translation function for XML-conformant strings

* --> I N <-- @p

* const TCHAR *pszIn - non-xml conformant string

* <-- OUT --> @r

* tstring - xml-conformant string

\*/

tstring CMobileProperty::sXmlTidy(const TCHAR *pszIn)

{

                tstring sOut;

 

                map<TCHAR,tstring> mapChar;

                map<TCHAR,tstring>::const_iterator iter;

                for (char c=' ';c <= '~';c++)

                                mapChar[c]=c;

                mapChar['&']=_T("&#38;");

                mapChar['€']=_T("&#8364;");

                mapChar['£']=_T("&#163;");

                mapChar['¿']=_T("&#191;");

                mapChar['¡']=_T("&#161;");

                mapChar['<']=_T("&#60;");

                mapChar['>']=_T("&#62;");

                mapChar['\"']=_T("&#34;");

                mapChar['à']=_T("&#224;");

                mapChar['À']=_T("&#192;");

                mapChar['á']=_T("&#225;");

                mapChar['Á']=_T("&#193;");

                mapChar['ä']=_T("&#228;");

                mapChar['Ä']=_T("&#196;");

                mapChar['ó']=_T("&#243;");

                mapChar['Ó']=_T("&#211;");

                mapChar['ö']=_T("&#246;");

                mapChar['Ö']=_T("&#214;");

                mapChar['©']=_T("&#169;");

                mapChar['è']=_T("&#232;");

                mapChar['È']=_T("&#200;");

                mapChar['é']=_T("&#233;");

                mapChar['É']=_T("&#201;");

                mapChar['ü']=_T("&#252;");

                mapChar['Ü']=_T("&#220;");

                mapChar['í']=_T("&#237;");

                mapChar['Í']=_T("&#205;");

                mapChar['ñ']=_T("&#241;");

                mapChar['Ñ']=_T("&#209;");

 

                for (int i=0;i < (int)strlen(pszIn);i++)

                {

                                //do not replace characters that seem wellformed already…

                                if (pszIn[i] == '&' && pszIn[i+1] == '#')

                                                sOut+=pszIn[i];

                                else

                                {              //we found a non wellformed character, replace it….

                                                iter=mapChar.find((TCHAR)pszIn[i]);

                                                if (iter != mapChar.end())

                                                                sOut+=iter->second;

                                                else

                                                {

                                                                TRACEIT2("illegal character code 0x%02X",pszIn[i]);

                                                }

                                }

                }

                return sOut;

Lamia Hannoun

unread,
Dec 22, 2010, 4:57:04 AM12/22/10
to iphonesdkd...@googlegroups.com
Thanks for your reply !

I was inspired by your idea to convert the NSdata that I get to nsstring (UTF8) in which I search for the special char and then I replace it by its code iso and I came up with this method 

-(NSString*)ConvertSpecialChar:(NSString *)stringToConvert

{

NSLog(@"%@",webData);

NSInteger i;

NSArray *clefs = [NSArray arrayWithObjects:@"&amp;",@"é",@"ô", nil];

NSArray *values = [NSArray arrayWithObjects:@"&#38;", @"&#233;",@"&#244;", nil];


NSDictionary *dict=[NSDictionary dictionaryWithObjects:values forKeys:clefs];


NSArray *keys = [dict allKeys];

for (i=0; i<[keys count]; i++) {

NSString *searchForMe = [keys objectAtIndex:i];

NSRange range = [stringToConvert rangeOfString : searchForMe];

if (range.location != NSNotFound) {

stringToConvert= [stringToConvert      stringByReplacingOccurrencesOfString:searchForMe  withString:[dict objectForKey:searchForMe]];

}

}

return stringToConvert;

}


and in my methode connectionDidFinishLoading

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

[connection release];

NSLog(@"Data has been loaded");

NSLog(@"DONE. Received Bytes: %d", [webData length]);

NSLog(@"%@",webData);

NSString *responseString = [[NSString allocinitWithData:webData encoding:NSUTF8StringEncoding];

if( xmlParser )

{

[xmlParser release];

}

NSString *conversion=[self ConvertSpecialChar:responseString];

NSLog(@"After conversion%@",conversion);

webData = [conversion dataUsingEncoding: NSASCIIStringEncoding];

NSLog(@"After conversion%@",webData);

xmlParser = [[NSXMLParser allocinitWithData: webData];

[xmlParser setDelegate: self];

[xmlParser setShouldResolveExternalEntities: YES];

[xmlParser parse];

NSLog(responseString);

[responseString release];

[webData release];


}


But I still get the same wrong response

After conversion<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetListServicesResponse xmlns="http://tempuri.org/"><GetListServicesResult xmlns:a="http://schemas.datacontract.org/2004/07/DtoModelLayer" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:ServiceDto><a:ReadOk>APR&#38;D</a:ReadOk><a:ServiceId>1</a:ServiceId><a:ServiceName>P&#244;le Etudes D&#233;v</a:ServiceName><a:WriteOk>AR&#38;Users</a:WriteOk></a:ServiceDto></GetListServicesResult></GetListServicesResponse></s:Body></s:Envelope>


2010-12-22 10:41:32.113 IphonePlanning[2081:207] s:Envelope

2010-12-22 10:41:32.113 IphonePlanning[2081:207] s:Body

2010-12-22 10:41:32.113 IphonePlanning[2081:207] GetListServicesResponse

2010-12-22 10:41:32.113 IphonePlanning[2081:207] GetListServicesResult

2010-12-22 10:41:32.114 IphonePlanning[2081:207] a:ServiceDto

2010-12-22 10:41:32.114 IphonePlanning[2081:207] a:ReadOk

2010-12-22 10:41:32.114 IphonePlanning[2081:207] APR

2010-12-22 10:41:32.114 IphonePlanning[2081:207] &

2010-12-22 10:41:32.114 IphonePlanning[2081:207] D

2010-12-22 10:41:32.114 IphonePlanning[2081:207] a:ServiceId

2010-12-22 10:41:32.114 IphonePlanning[2081:207] 1

2010-12-22 10:41:32.115 IphonePlanning[2081:207] a:ServiceName

2010-12-22 10:41:32.115 IphonePlanning[2081:207] P

2010-12-22 10:41:32.115 IphonePlanning[2081:207] ô

2010-12-22 10:41:32.115 IphonePlanning[2081:207] le Etudes 

2010-12-22 10:41:32.115 IphonePlanning[2081:207] &

2010-12-22 10:41:32.115 IphonePlanning[2081:207]  D

2010-12-22 10:41:32.115 IphonePlanning[2081:207] é

2010-12-22 10:41:32.115 IphonePlanning[2081:207] v

2010-12-22 10:41:32.116 IphonePlanning[2081:207] a:WriteOk

2010-12-22 10:41:32.116 IphonePlanning[2081:207] AR

2010-12-22 10:41:32.116 IphonePlanning[2081:207] &

2010-12-22 10:41:32.116 IphonePlanning[2081:207] Users


I don't know maybe I did someth wrong in my methods?Any Help
Thanks in advance !
 
2010/12/21 Till Toenshoff <m...@mmsguru.de>
Reply all
Reply to author
Forward
0 new messages