转载于:http://blog.csdn.net/crayondeng/article/details/8738768

下面简单介绍如何通过url获取xml的两种方式。

第一种方式相对简单,使用NSData的构造函数dataWithContentsOfURL;不多解释,直接上代码咯。

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

上面的  NSData *xmlData = [NSData dataWithContentsOfURL:url]; 就是获取xml的关键啦。

注意:如果直接输出xmlData的话会是一对unicode,所以用UTF-8编码转换成是String进行输出就好啦。

第二种方式:通过NSURLConnection建立网络连接,并实现它的几个委托方法,从而获取数据。

代码如下,应该可以看懂的。

  1. @implementation ViewController {
  2. BOOL getXmlDone;  //是否停止获取xml数据的标志
  3. NSMutableData *xmlData;  //存储获得的xml数据
  4. }
  5. //自定义的一个方法
  6. - (void) getXML {
  7. //获取xml
  8. xmlData = [NSMutableData data];
  9. //Clears the receiver’s cache, removing all stored cached URL responses.
  10. //清除接收器的缓存,删除所有存储的高速缓存的URL的响应。
  11. [[NSURLCache sharedURLCache] removeAllCachedResponses];
  12. NSURL *url = [NSURL URLWithString:@"http://222.73.161.212/ispace2/servlet/com.lemon.xml.XmlAction"];
  13. NSURLRequest *theRequest = [NSURLRequest requestWithURL:url];
  14. //create the connection with the request and start loading the data
  15. NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  16. [self performSelectorOnMainThread:@selector(downloadStarted) withObject:nil waitUntilDone:NO];
  17. if (urlConnection != nil) {
  18. do {
  19. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  20. } while (!getXmlDone);
  21. }
  22. }
  23. #pragma mark NSURLConnection Delegate methods
  24. - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
  25. return nil;
  26. }
  27. // Forward errors to the delegate.
  28. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  29. getXmlDone = YES;
  30. }
  31. // Called when a chunk of data has been downloaded.
  32. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  33. // Append the downloaded chunk of data.
  34. [xmlData appendData:data];
  35. }
  36. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  37. [self performSelectorOnMainThread:@selector(downloadEnded) withObject:nil waitUntilDone:NO];
  38. getXmlDone = YES;
  39. }
  40. - (void)downloadStarted {
  41. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  42. }
  43. - (void)downloadEnded {
  44. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  45. }
  46. - (void)viewDidLoad
  47. {
  48. [super viewDidLoad];
  49. // Do any additional setup after loading the view, typically from a nib.
  50. [self getXML];
  51. NSString *xmlString = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
  52. NSLog(@"data: %@",xmlString);
  53. }
  54. - (void)didReceiveMemoryWarning
  55. {
  56. [super didReceiveMemoryWarning];
  57. // Dispose of any resources that can be recreated.
  58. }
  59. @end

小结一下,第一种简单,一次性获取所有的xml数据,第二种有点复杂,当然其灵活性也更好。

要验证获取的数据的准确性的话,就点击你的url吧!http://222.73.161.212/ispace2/servlet/com.lemon.xml.XmlAction

OK,下一篇文章将会介绍如何解析xml。come on!

iOS 通过URL网络获取XML数据的两种方式的更多相关文章

  1. SparkStreaming获取kafka数据的两种方式:Receiver与Direct

    简介: Spark-Streaming获取kafka数据的两种方式-Receiver与Direct的方式,可以简单理解成: Receiver方式是通过zookeeper来连接kafka队列, Dire ...

  2. 工具篇-Spark-Streaming获取kafka数据的两种方式(转载)

    转载自:https://blog.csdn.net/weixin_41615494/article/details/7952173 一.基于Receiver的方式 原理 Receiver从Kafka中 ...

  3. Spark-Streaming获取kafka数据的两种方式:Receiver与Direct的方式

    简单理解为:Receiver方式是通过zookeeper来连接kafka队列,Direct方式是直接连接到kafka的节点上获取数据 Receiver 使用Kafka的高层次Consumer API来 ...

  4. spark-streaming获取kafka数据的两种方式

    简单理解为:Receiver方式是通过zookeeper来连接kafka队列,Direct方式是直接连接到kafka的节点上获取数据 一.Receiver方式: 使用kafka的高层次Consumer ...

  5. jQuery异步获取json数据的2种方式

    jQuery异步获取json数据有2种方式,一个是$.getJSON方法,一个是$.ajax方法.本篇体验使用这2种方式异步获取json数据,然后追加到页面. 在根目录下创建data.json文件: ...

  6. 使用web.xml方式加载Spring时,获取Spring context的两种方式

    使用web.xml方式加载Spring时,获取Spring context的两种方式: 1.servlet方式加载时: [web.xml] <servlet> <servlet-na ...

  7. easyUI之datagrid绑定后端返回数据的两种方式

    先来看一下某一位大佬留下的easyUI的API对datagrid绑定数据的两种方式的介绍. 虽然精简,但是,很具有“师傅领进门,修行靠个人”的精神,先发自内心的赞一个. 但是,很多人和小编一样,第一次 ...

  8. SparkStreaming与Kafka,SparkStreaming接收Kafka数据的两种方式

    SparkStreaming接收Kafka数据的两种方式 SparkStreaming接收数据原理 一.SparkStreaming + Kafka Receiver模式 二.SparkStreami ...

  9. strus2中获取表单数据 两种方式 属性驱动 和模型驱动

    strus2中获取表单数据 两种方式 属性驱动 和模型驱动 属性驱动 /** * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值 * 如果一个属性在对象栈,在页面 ...

随机推荐

  1. CF 148D Bag of mice 概率dp 难度:0

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  2. Xilinx Microblaze Bootloader

    作者:Hello,Panda 一般而言,Xilinx Microblaze会被用来在系统中做一些控制类和简单接口的辅助性工作,比如运行IIC.SPI.UART之类的低速接口驱动,对FPGA逻辑功能模块 ...

  3. Android EditText 中hint文字大小以及与输入文字颜色保存一致

    SpannableString 这个就是用来处理android 文本信息 可编辑 可点击 感兴趣的自己去看! /* * Copyright (C) 2006 The Android Open Sour ...

  4. MarkDown格式作业模板

    发布的随笔可复制下面的MarkDowm模板 注意事项 标题第XX次作业替换成相应的第一次作业.第二次作业...... 代码托管的链接一定要换成自己的项目 码云提交历史截图必须是自己每周的提交截图 #& ...

  5. 那些实用的Nginx规则

    1. 概述 大家都知道Nginx有很多功能模块,比如反向代理.缓存等,这篇文章总结下我们这些年实际环境中那些有用的Nginx规则和模块,大部分是用法的概括及介绍,具体细节在实际配置时再自行google ...

  6. HDU 4825 字典树

    HDU 4825 对于给定的查询(一个整数),求集合中和他异或值最大的值是多少 按位从高位往低位建树,查询时先将查询取反,然后从高位往低位在树上匹配,可以匹配不可以匹配都走同一条边(匹配表示有一个异或 ...

  7. TimeExit 界面无点击定时退出类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  8. ubuntu 16.04安装HUSTOJ过程

    一.背景介绍: 因为工作需要,想在学校搭建一个OJ平台用于程序测试与评价.于是需要搭建oJ,由于之前都是在云端服务器搭建系统,没有在实际服务器平台搭建过,所以遇到不少坑,都靠自己来填补.故而写下此教程 ...

  9. 基数排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def RadixSort(a): i = 0 #初始为个位排序 n = 1 #最小的位数置为1(包含0) max ...

  10. 微信小程序设置底部导航栏目方法

    微信小程序底部想要有一个漂亮的导航栏目,不知道怎么制作,于是百度找到了本篇文章,分享给大家. 好了 小程序的头部标题 设置好了,我们来说说底部导航栏是如何实现的. 我们先来看个效果图 这里,我们添加了 ...