hi there I am working on a program that sends requests via XML Webservice and parses the data using NSXML parser.
I've used that method for anything on my program and it works flawlessly until I had this:
NSXML Parser returns data and parses when the URL is like that:
http://someasp.netserver/xmlservices/service1.asmx/getSales?date1=01.03.2011%2000:00:00&date2=%2023.03.2011%2000:00:00%20&Name=JOHN
but when I change the URL to this:
http://someasp.netserver/xmlservices/service1.asmx/getSales?date1=01.03.2011%2000:00:00&date2=%2023.03.2011%2000:00:00%20&Name=JÜRGEN
it doesn't return anything.
I copy and paste those URL addresses into a web browser and I can see the XML results but on the iPhone NSXMLparser just ignores the parsing and returns nothing.
I have a list of 15 "Name" values in my program, and whenever the name contains characters like 'ö,ü,ş,İ,ç' it just returns zero, otherwise it's working normally.
Thanks for helping.
Also, here's my parsing code in the program :
NSURL *URL=[[NSURL alloc] initWithString:testURL]; //test url is the request string
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL :URL];
[parser setDelegate:self];
[parser parse];
You need to perform URL encoding, you can't just shove special characters into a URL as-is.
Please see this question:
iPhone URL encoding Problem
Or search for "iphone url encoding".
Related
I used NSXML parser to parse a SOAP response XML i recieved from the web service.
In my root method,
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
I used this code to send my SOAP request, where theRequest variable has my SOAP request.
so after the data is recieved ,
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//codes to recieve webData
xmlParser = [[NSXMLParser alloc] initWithData: _webData];
[_xmlParser setDelegate: self];
[_xmlParser setShouldResolveExternalEntities: YES];
[_xmlParser parse];
}
Now, the program flows to didStartElement , didFinishDocument methods. My root method needs to return the result obtained after parsing the xml , but when I checked the program flow using breakpoints, the parsing methods doesnot end before the return statement is called in my code and hence I am not able to return the parsed values. How do I solve this?
NSXMLParser works completely synchronous. When [_xmlParser parse] returns, all the parsing is done (or aborted with an error).
According your description, the problem is not quite clear. It seems, the problem is, that - after the root element has been constructed - you continue to process your program while still being within the NSXMLParser's parse method.
To solve this, just invoke a selector on the main thread passing the parser's delegate result (root element) to a method which eventually handles it. Alternatively, use dispatch_async to the main thread, where you invoke a method which handles the root element.
I'd like to develop a very simple "bus timetable" app that loads a particular URL and can use the HTML to figure out bus numbers, their routes and expected times. Unfortunately it's a very simple website, so the data is spread across TD and DIV fields, rather than in xml.
Can anyone provide some pointers on where to start? I've had a look at NSURL, NSURLConnection and the like, and am able to download the contents of an HTML file, but I'm unsure what to do next.
Many thanks.
There are many ways to accomplish this task. Ultimately, you will want to retrieve something an easily-processed format other than raw html. Here is one way to do it:
I would recommend writing a server-side script that converts the html into an easily-digestible format, such as JSON.
If you have php experience, you write a script / web-service that grabs the needed elements and place the content in an associative array. Place the script on your server and have your application call the web-service URL when ready to retrieve the info.
Finally, return the information as a JSON object and parse the JSON into your app.
<?php
//parse html into the $array variable.
$array = json_encode($array);
echo $array;
?>
I would recommend using the SBJSON library for parsing the JSON object: Take a look at SBJSON
Initiate your NSURL request by calling the script URL.
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.yoursite/script.php"]];
If the request is successful, parse the resulting JSON: (assuming use of SBJSON library)
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];
[responseData release];
NSError *error;
SBJSON *json = [[[SBJSON alloc] init]autorelease];
NSDictionary *busData = [json objectWithString:responseString error: &error];
//busData contains bus data that you formatted...
Beware, this is assuming that you want to add your data to a dictionary and then display it appropriately. I consider this a mere snippet for formatting data in a way you desire. Parsing all of the html within the app may difficult and that is why I advocate formatting the data as JSON first. If you decide to go the XML route, there are several XML parsers as well.
I've webservice which converts CelsiusToFahrenheit. This webservice response is in string format instead of xml so how can I display a response in a label programmatically?
Is there a sample available for that?
The simplest method is a one-liner:
NSError *error = nil;
myLabel.text = [NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
As with most one-line solutions, there's a caveat. This method blocks the current thread until the request completes or fails, so you'd better use it in conjunction with performSelectorInBackground:withObject: to avoid locking UI.
Here is a tutorial that I used to retrieve data from a web service using the requestWithURL method of NSURLRequest:
http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/
The example is for JSON, but you can just omit all of the JSON stuff from the tutorial, since the string response is going to be fed into your connectionDidFinishLoading method.
Im working on simle RSS reader. This reader loads data from internet via this code:
NSXMLParser *rss = [[NSXMLParser alloc] initWithURL:[NSURL URLWithString:#"http://twitter.com/statuses/user_timeline/50405236.rss"]];
My problem is with encoding. RSS 2.0 file is supposed to be UTF8 encoded according to encoding attribute in XML file.
<?xml version="1.0" encoding="utf-8"?>
So when I download URLs content I get text truncated after first occurance of char with diacritics, example: ľ š č ť ž ý á í é, etc.
I tried to solve the problem by downloading URL as UTF8 string, I used this code:
NSString *rssXmlString = [NSString stringWithContentsOfURL: [NSURL URLWithString: #"http://www.macblog.sk/rss.xml"] encoding:NSUTF8StringEncoding error: nil];
NSData *rssXmlData = [rssXmlString dataUsingEncoding: NSUTF8StringEncoding];
Did not help. Thanx for your responses.
Check out MWFeedParser on GitHub, it's an open source RSS/Atom feed parser I've released and it makes reading and parsing web feeds extremely easy.
There's a simple demo app too which shows how easy it is to implement.
Hope this might be of some use!
I'm working with the Google Docs API in my iPhone application. I've sent the proper POST request, and received a response along with some NSData. If I NSLog the NSData object, I get this:
NSData *returnedData = [NSURLConnection sendSynchronousRequest:request returningResponse:theResponse error:NULL];
NSLog(#"%#",returnedData);
//output : <4572726f 723d4261 64417574 68656e74 69636174 696f6e0a>
I think the NSData is either an NSDictionary or an NSArray. I'm supposed to receive a couple of items back, an SID, an LSID, and an Auth.
If I could turn the NSData chunk into an NSDictionary I could just find the object for whichever key.
Can anyone help?
You will have to decode the data. Try the following:
NSString *result= [[NSString alloc] initWithData:returnedData encoding:NSASCIIStringEncoding];
Of course this will depend on how the data is encoded. NSUTF8StringEncoding might work instead.
You may just need to pass the data to an NSXMLParser
The best way to deal with web data is Doing NSXML parsing. No need of decoding data the parser will handle itself