I have a XML parser. I'm getting the XML file from server and write that XML file in to a local file in cache. Before do that, I want to check the URL has the XML file. How Can I check the available URL's page is a XML page or another type of page(Ex:HTML,PHP)?? Simply how can I identify a XML file ??
Ultimately, you have to look at the contents of the data retrieved to make sure it's valid XML, and parsing is the easiest way to do that.
If you're retrieving the data via a HTTP request, you can, though, also look at the response you receive before you start receiving the actual data. For example, if using NSURLConnection, you can implement a didReceiveResponse, which should often return a 200 for status code and text/xml for content type:
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if ([response isKindOfClass:[NSHTTPURLResponse class]])
{
NSHTTPURLResponse *httpResponse = (id)response;
NSInteger statusCode = httpResponse.statusCode;
NSString *contentType = httpResponse.allHeaderFields[#"Content-Type"];
NSLog(#"%d; %#", statusCode, contentType);
// check to see if statusCode == 200 and/or [contentType isEqualToString:#"text/xml"] here;
}
}
As an aside, the status code and the content type are set by the server, so it is, admittedly, dependent upon the server's implementation (e.g. if the XML is being generated programmatically by the server, hopefully it's setting these HTTP response fields correctly, but if you're retrieving XML from third party servers, you can't be guaranteed that they're well-behaved). But a status code of 200 and content type of "text/xml" are customary and most servers will set these values appropriately if you're just retrieving a XML file.
The most reliable technique for validating your XML is to just receive the data from the server, and submit it to a parser, and see if the parser returns an error or not.
There are various solution available for this:
http://knol2share.blogspot.in/2009/05/validate-xml-against-xsd-in-c.html
http://wiki.njh.eu/XML-Schema_validation_with_libxml2
Checking for proper xml before parsing in NSXMLParser
Hope this will help you.
Related
In my app i am using WSDl webservice.And try to call webservice method.
In that request send successfuly and also get response.But when i fetch that response using following method:
// Called when the connection has finished loading.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError* error;
if(self.logging == YES) {
NSString* response = [[NSString alloc] initWithData: self.receivedData encoding: NSUTF8StringEncoding];
NSLog(#"response=%#", response);
[response release];
}
It gives following exception.
And my NSLog statement shows following details:
response=<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><soapenv:Fault><faultcode>soapenv:Server</faultcode><faultstring>org.apache.axis2.databinding.ADBException: Unexpected subelement {http://services.webservices.sparta.com}sSessionId</faultstring><detail /></soapenv:Fault></soapenv:Body></soapenv:Envelope>
Please suggest me the way to solve this issue.
Thank you.
From the question description we can not exactly figure out problem.
But from your log I can give you some suggestion to check your code :
<soapenv:Fault><faultcode>soapenv:Server</faultcode><faultstring>
1) You may be passing wrong number of parameters to web-service.
2) The parameter data type may not be matching. for example, you are passing string where server accepting integer or vice-versa.
3) Or web-service having some problem.
you can also search on org.apache.axis2.databinding.ADBException: Unexpected subelement. There are many useful links which may help you to figure out whether problem is at your web-service or in your iphone coding.
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 am creating an app in which i want to take xmldata using webservices.
i am using NSXMLParser.
i get the data in respond data when this below method calling
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//NSLog(#"====== Connection ========");
[catalogData appendData:data];
//NSLog(#"catalog data-=-=-=-=-=-=-%#", catalogData);
}
but when i parse this catalogData in finishloading method i get the null array.
i don't know what the problem is,
this is my XML data
3
1
1
<sPagRichiestaFornitore>
</sPagRichiestaFornitore>
<idCategoria>1</idCategoria>
NSLog your data inside connectionfinishloading then. If you get a proper XML, then you can create the XML parser accordingly.
You can refer the Top Paid Sample app of apple for the NSXMLParser Implementation.
i cannot help with such a small code friend . can you please tell what exactly you get in response if you not getting please check your php file that is on server some problem with that . One thing i suggest you is that once remove this tag from your php file and again try to get response
thanks
hope this helps
Currently I'm using Resty (http://projects.lukeredpath.co.uk/resty/) to build my own API wrapper for Objective-C.
I digress however, in the request itself, I am able to determine what is actually being sent and it seems that the NSString *email (I scan for an # to make sure it is a legitimate email before storing in another NSString) is actually changed.
An example, joe#example.com has been scanned for the #, and then sent to the NSString *email for the request. However, when I send the actual GET request, the NSString is changed to joe%40example.com.
Is this due to the Resty request, or do I have to encode/decode the NSString before the request?
Yes, you should URL decode and then perform any validation tasks.
Here is what their documentation has:
NSData or any other data-encodable
payloads set the request body as-is;
encodable payloads will be encoded
using UTF8
http://projects.lukeredpath.co.uk/resty/documentation.html
I want to check the dates on files -- zip, jpg, or whatever -- on my server and, if they are newer than the copies on my iPhone, to download them.
I wrote the following method based on a post here that's about a year old. It has two problems:
+ (NSString *) f_GetServerFileDate:(NSString *)MyURL {
NSURL *oURL = [NSURL URLWithString:MyURL];
NSURLRequest *oRequest = [NSURLRequest requestWithURL:oURL];
NSHTTPURLResponse *oResponse;
[NSURLConnection sendSynchronousRequest:oRequest returningResponse:&oResponse error:nil];
if ( [oResponse respondsToSelector:#selector( allHeaderFields )] ) {
NSDictionary *metaData = [oResponse allHeaderFields];
return [metaData objectForKey:#"Last-Modified"];
} else {
return #"00000000";
}
}
Problem 1: It returns "00000000" when given "http://www.mysite.com/myzip.zip" as a URL.
Problem 2: For an Active Server Page (just a test; not that I'd really download one) it returns a date that has no bearing on the date the file was last uploaded or modified.
What's the right way?
This is probably less a problem of your iPhone code but rather of the web server. A web server is not required to include the Last-Modified header attribute. And for dynamic pages (such as ASP pages) it is correct to return the date when the page was dynamically generated (i.e. the current date) and not the date when the page was developed or deployed.
I suggest you use a browser extension such as Live HTTP headers for Fireofx to observe what HTTP header attributes the web server returns. If the date is missing, you'll be out of luck (unless you have access to the web server and can fix it there).
Furthermore, your code will always download the image no matter when it has been last modified. You can prevent this if you include the HTTP header If-Modified-Since in your request. That way the web server will send the image if it has been modified since the specified date or just send a 304 (Not modified) result code if the image is still up to date. But again: It depends on the web server if this option is supported and it only works for static content unless the author of the web application has specifically implemented it.