Reputation: 2741
I am working to download and parse data from a server.
avery thing is fine , but i found it is not taking some strings completely as they are,
after six hour of search :( I'm unable to find the issue.
i have to parse the string which contains & sign. and at back end (server) i just change it to & ; ,so that NSXML Parser can parse it without any issue.
Example String
Hello this is & my test string.
what is all happening is,
i am just getting
Hello this is
rest string is skipping.
here is my code
message = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<XMLBody>"
"<Head>"
"<Document>"
"<DocType>Query</DocType>"
"<DocDate>%@</DocDate>"
"</Document>"
"<UserName>####</UserName>"
"<Password>####</Password>"
"<Database>%@</Database>"
"</Head>"
"<Body>"
"<Query>%@</Query>"
"</Body>"
"</XMLBody>",currentDate,myDBName,myQuery];
uRL = [NSURL URLWithString:@"http://www.myserverpath"];
request = [NSMutableURLRequest requestWithURL:uRL];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]];
errorMsg = nil;
if (true)
{
NSLog(@"Connection Build and waitng for response");
xmldata = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ];
errorParsing=NO;
NSString *data = [[NSString alloc] initWithData:xmldata encoding:NSASCIIStringEncoding];
xmldata = [data dataUsingEncoding:NSUTF8StringEncoding];
NSLog("Result is %@",data); // here every thing is just perfect.
xmlParser = [[NSXMLParser alloc] initWithData:xmldata];
[xmlParser setDelegate:self];
[xmlParser parse];
}
in above NSLog everything is just perfect.
and when i reach here
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[elementValue appendString:string];
NSLog("String is = %@",string); // here i got half string
[string stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
// convert again into & sign
// doing my other stuff.
}
in this nslog just getting half of string.
what should i do get complete string as
Hello this is & my test string.
Upvotes: 0
Views: 144
Reputation:
This will surely resolve your problem :)
I am posting it from my App. On server side i just replace all the invalid / special characters with these :-
From this link http://en.wikipedia.org/wiki/Character_encodings_in_HTML
just figured out & → & (ampersand, U+0026)
so i use a trick of using number 26 with % mark enclosing in parantheses to encode these sensitive values.
& = [%26], > = [%3E], < = [%3C], ' = [%27], \ = [%22]
get data with your URLRequest and replace these characters again in this delegate method
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[elementValue appendString:string];
string = [string stringByReplacingOccurrencesOfString:@"[%26]" withString:@"&"];
string = [string stringByReplacingOccurrencesOfString:@"[%3E]" withString:@">"];
string = [string stringByReplacingOccurrencesOfString:@"[%3C]" withString:@"<"];
string = [string stringByReplacingOccurrencesOfString:@"[%27]" withString:@"'"];
string = [string stringByReplacingOccurrencesOfString:@"[%22]" withString:@"\""];
}
Upvotes: 1