IOS uses the URL network to obtain XML data.

Source: Internet
Author: User

The following describes how to obtain XML using URLs.

The first method is relatively simple. You can use the nsdata constructor datawithcontentsofurl. If you don't have much to explain, you can directly use the code.

    NSURL *url = [NSURL URLWithString:@"http://222.73.161.212/ispace2/servlet/com.lemon.xml.XmlAction"];         //A Boolean value that turns an indicator of network activity on or off.    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;        NSData *xmlData = [NSData dataWithContentsOfURL:url];        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;        NSString *xmlString = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];        if (xmlData == nil) {        NSLog(@"File read failed!:%@", xmlString);    }    else {        NSLog(@"File read succeed!:%@",xmlString);    }

The above nsdata * xmldata = [nsdata datawithcontentsofurl: url];
Is the key to getting XML.

NOTE: If xmldata is output directly, it will be a pair of Unicode, so it is good to convert it to string with UTF-8 encoding.


Method 2: Establish a network connection through nsurlconnection and implement several delegate methods to obtain data.

The Code is as follows.

@ Implementation viewcontroller {bool getxmldone; // indicates whether to stop obtaining XML data. nsmutabledata * xmldata; // stores the obtained XML data} // a custom method-(void) getxml {// get xmldata = [nsmutabledata data]; // clears the handler's cache, removing all stored cached URL responses. // clear the cache of the receiver and delete the response of all cached URLs. [[Nsurlcache sharedurlcache] removeallcachedresponses]; nsurl * url = [nsurl urlwithstring: @ "http: // 222.73.161.212/ispace2/servlet/COM. lemon. XML. xmlaction "]; nsurlrequest * therequest = [nsurlrequest requestwithurl: url]; // create the connection with the request and start loading the data nsurlconnection * urlconnection = [[nsurlconnection alloc] initwithrequest: therequest delegate: Self]; [self perform Selectoffmainthread: @ selector (downloadstarted) withobject: Nil waituntildone: No]; If (urlconnection! = Nil) {do {[[nsunloop currentrunloop] runmode: nsdefaultrunloopmode beforedate: [nsdate distantfuture];} while (! Getxmldone) ;}# Pragma mark nsurlconnection delegate methods-(nscachedurlresponse *) connection :( nsurlconnection *) connection willcacheresponse :( nscachedurlresponse *) cachedresponse {return nil ;} // forward errors to the delegate. -(void) connection :( nsurlconnection *) connection didfailwitherror :( nserror *) error {getxmldone = yes;} // called when a chunk of data has been downloaded. -(void) connection :( nsurlconnection *) connection didreceivedata :( nsdata *) Data {// append the downloaded chunk of data. [xmldata appenddata: Data];}-(void) connectiondidfinishloading :( nsurlconnection *) connection {[self defined mselec1_mainthread: @ selector (downloadended) withobject: Nil waituntildone: No]; getxmldone = yes;}-(void) downloadstarted {[uiapplication sharedapplication]. networkactivityindicatorvisible = yes;}-(void) downloadended {[uiapplication sharedapplication]. networkactivityindicatorvisible = no;}-(void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view, typically from a nib. [self getxml]; nsstring * xmlstring = [[nsstring alloc] initwithdata: xmldata encoding: nsutf8stringencoding]; nslog (@ "data: % @", xmlstring);}-(void) didreceivememorywarning {[Super didreceivememorywarning]; // dispose of any resources that can be recreated .} @ end

To sum up, the first method is simple. It gets all the XML data at a time. The second method is a bit complicated. Of course, its flexibility is also better.

To verify the accuracy of the obtained data, click your URL! Http: // 222.73.161.212/ispace2/servlet/COM. Lemon. xml. xmlaction

OK. The next article will introduce how to parse XML. Come on!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.