问题描述
- 不使用分析程序在XMLNSString中提取值
-
string如下:<?xml version="1.0" encoding="utf-8"?> <NewDataSet> <Table> <CITY>Jupiter</CITY> <STATE>FL</STATE> <ZIP>33477</ZIP> <AREA_CODE>561</AREA_CODE> <TIME_ZONE>E</TIME_ZONE> </Table> </NewDataSet>
我想要获取其中的city,也就是Jupiter,但是不用xml分析器,有没有简捷的方法实现?
解决方案
NSArray *array = [yourXMLString componentsSeparatedByString:@"CITY"];
NSString *city = [[array objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"></"]];
其实还是分析器好用点
解决方案二:
NSString* str=[[NSString alloc]initWithContentsOfFile: FILENAME encoding: NSUTF8StringEncoding error: nil];
NSLog(@"%@",str);
NSRange range1= [str rangeOfString: @"<CITY>"];
NSRange range2= [str rangeOfString: @"</CITY>"];
NSUInteger location= range1.location+range1.length;
NSUInteger length= range2.location-range1.location-range1.length;
NSString* substr= [str substringWithRange: NSMakeRange(location,length)];
NSLog(@"%@",substr);
时间: 2025-01-30 06:07:51