iphone - Parsing Local XML File Works, Downloaded XML File Doesn't -


i have problem when fetch xml file internet , parse it, error:

error while parsing document: error domain=smxmldocumenterrordomain code=1 "malformed xml document. error @ line 1:1." userinfo=0x886e880 {linenumber=1, columnnumber=1, nslocalizeddescription=malformed xml document. error @ line 1:1., nsunderlyingerror=0x886e7c0 "the operation couldn’t completed. (nsxmlparsererrordomain error 5.)"}

here extract code (i believe showing relevant code, if need more, please ask.)

// create url request , set url nsurl *url = [nsurl urlwithstring:@"http://***.xml"]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url];  // display network activity indicator [[uiapplication sharedapplication] setnetworkactivityindicatorvisible:yes];  // perform request on new thread don't block ui dispatch_queue_t downloadqueue = dispatch_queue_create("download queue", null); dispatch_async(downloadqueue, ^{      nserror* err = nil;     nshttpurlresponse* rsp = nil;      // perform request synchronously on thread     nsdata *rspdata = [nsurlconnection sendsynchronousrequest:request returningresponse:&rsp error:&err];      // once response received, handle on main thread in case ui updates     dispatch_async(dispatch_get_main_queue(), ^{         // hide network activity indicator         [[uiapplication sharedapplication] setnetworkactivityindicatorvisible:no];          if (rspdata == nil || (err != nil && [err code] != noerr)) {             // if there no data received, or error...             nslog(@"no data received.");         } else {             // cache file in cache directory             nsarray* paths = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes);             nsstring* path = [[paths objectatindex:0] stringbyappendingpathcomponent:@"init.xml"];              //nslog(@"%@",path);             [[nsfilemanager defaultmanager] removeitematpath:path error:nil];              [data writetofile:path atomically:yes];              //nsstring *samplexml = [[nsbundle mainbundle] pathforresource:@"sample" oftype:@"xml"];               nsdata *data = [nsdata datawithcontentsoffile:path];              // create new smxmldocument contents of sample.xml             nserror *error;             smxmldocument *document = [smxmldocument documentwithdata:data error:&error];              // check errors             if (error) {                 nslog(@"error while parsing document: %@", error);                 // return;              } 

firstly, have connected iphone xml feed has fetched , written path of variable path. check errors in xml document , error every time.

however, if use local xml file have placed in main folder of application there no problem fetching data.

using code:

nsstring *samplexml = [[nsbundle mainbundle] pathforresource:@"sample" oftype:@"xml"]; 

so have idea can have done wrong? seems if doesn't download , store xml file iphone's cache, nslog(); seems show differently. local file same file on internet.

furthermore, tried save file path without results, though.

a couple of observations:

  1. the key issue appear retrieved data in rspdata, when write temporary file, you're writing data, not rspdata. change line says:

    [data writetofile:path atomically:yes]; 

    to

    [rspdata writetofile:path atomically:yes]; 

    frankly, don't see data variable defined @ point (do have ivar lingering about?). i'd ruthless getting rid of ivars or other variables don't need, don't accidentally refer unused variable. anyway, use rspdata retrieved rather other variable.

  2. why writing file, read file nsdata pass xml parser? seems entirely unnecessary. go ahead , use rspdata retrieved. if want save nsdata file can examine later debugging purposes, that's fine. there's no point in re-retrieving nsdata file, have in rspdata already.

  3. if encounter these errors in future, feel free examine contents of nsdata variable debugging line of code, like:

    nslog(@"rspdata = %@", [[nsstring alloc] initwithdata:rspdata encoding:nsutf8stringencoding]); 

    when that, can @ string rendition of nsdata, , problem become self evident.

  4. as complete aside, in debugging error handler, have line says:

    nslog(@"no data received."); 

    i might suggest include errors might provided, e.g.:

    nslog(@"no data received: error = %@", err); 

    ios provides useful error messages, should avail of those.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -