xml文件结构如下
@(〓〓 iOS-实用技术)[JSON/XML 数据解析]
php代码解析:
目录
$url="";//输出xml的URL
$dom = new DOMDocument('1.0', 'utf-8');
$dom->load($url);
这样我们捕获里XML数据,然后一点点截取,这段XML有多个URL节点,需要利DOMPATH解析
本文JSON数据解析将介绍TouchJson、SBJson 、JSONKit和iOS5所支持的原生的json方法,解析国家气象局API,TouchJson和SBJson需要下载他们的库.
接口地址有两个:
http://www.weather.com.cn/data/sk/101010100.html
http://www.weather.com.cn/data/cityinfo/101010100.html
解析速度
由高到低: NSJSONSerialization -> JSONKit -> SBJson -> TouchJSon
$xpath = new DOMXPath($dom);
$node = '//url';
$length = $xpath->query($node)->length;
- (IBAction)btnPressIOS5Json:(id)sender {
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101180601.html"]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );
}
本文由宝马娱乐在线发布于网络频道,转载请注明出处:宝马娱乐在线php 解析XML DOMXPath的例子