使用cocoapods工具導(dǎo)入工程
<pre> pod 'KissXML', '~> 5.1.2'</pre>
利用kissXML創(chuàng)建一個(gè)XML文件(如地圖的GPX信息文件)
<pre>
// 1.判斷加速計(jì)是否可用
if (!self.motionManager.isAccelerometerAvailable) {
myLog(@"加速計(jì)不可用");
return;
}
// 2.設(shè)置采樣間隔
self.motionManager.accelerometerUpdateInterval = 1;
NSString *filePath = [FilePathClass filePathForFileName:@"location.gpx" andIsLog:YES]; //得到gpx文件路徑
if (![FilePathClass filePathIsExisted:@"location.gpx"]) { //本地不存在該文件
if ([[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]) {
myLog(@"gpx創(chuàng)建文件成功");
}else{
[FilePathClass filePathDeleteFileName:@"location.gpx"];
myLog(@"gpx文件刪除成功");
if ([[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]) {
myLog(@"gpx文件創(chuàng)建成功");
}
}
}
//+++++++++++++++++++++++創(chuàng)建XML元素節(jié)點(diǎn)++++++++++++++++++++++++++++++
//創(chuàng)建gpx文件后
//XML頭字符串
__block NSString *xmlString = @"<?xml version="1.0"?>\n";
//gpx根節(jié)點(diǎn)
DDXMLElement *gpxElement = [[DDXMLElement alloc] initWithName: @"gpx"];
[gpxElement addAttributeWithName:@"version" stringValue:@"1.1"];
[gpxElement addAttributeWithName:@"creator" stringValue:@"ios"];
// 3.開始采樣
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { // 當(dāng)采樣到加速計(jì)信息時(shí)就會執(zhí)行
if (error) return;
// 4.獲取加速計(jì)信息
CMAcceleration acceleration = accelerometerData.acceleration;
NSString *x = [NSString stringWithFormat:@"%.3f",acceleration.x];
NSString *y = [NSString stringWithFormat:@"%.3f",acceleration.y];
NSString *z = [NSString stringWithFormat:@"%.3f",acceleration.z];
//wpt子節(jié)點(diǎn)
DDXMLElement *wptElement = [[DDXMLElement alloc] initWithName: @"wpt"];
DDXMLElement *timeElement = [[DDXMLElement alloc] initWithName: @"time"];
[timeElement setStringValue: TIME];
[wptElement addChild:timeElement];
DDXMLElement *xElement = [[DDXMLElement alloc] initWithName: @"x"];
[xElement setStringValue: x];
[wptElement addChild:xElement];
DDXMLElement *yElement = [[DDXMLElement alloc] initWithName: @"y"];
[yElement setStringValue: y];
[wptElement addChild:yElement];
DDXMLElement *zElement = [[DDXMLElement alloc] initWithName: @"z"];
[zElement setStringValue: z];
[wptElement addChild:zElement];
[gpxElement addChild: wptElement];
}];
//+++++++++++++++++++++++向XML頭追加節(jié)點(diǎn)字符串+++++++++++++++++++++++
xmlString = [xmlString stringByAppendingString:[gpxElement XMLString]];
//+++++++++++++++++++++++寫到本地文件+++++++++++++++++++++++
[xmlString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//+++++++++++++++++++++++讀取本地文件中的內(nèi)容+++++++++++++++++++++++
NSString *str = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:filePath] encoding:kCFStringEncodingUTF8 error:nil];
myLog(@"xmlStr:%@",str);
</pre>
讀取解析XML文件
<pre>
//獲取xml路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"location.gpx" ofType:nil];
NSString *xmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
DDXMLDocument *doc = [[DDXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil];
//開始解析
NSArray *children = [doc nodesForXPath:@"http://wpt" error:nil];
//遍歷每個(gè)元素
for (DDXMLElement *obj in children) {
NSString *time =[[obj attributeForName:@"time"] stringValue];
NSString *x =[[obj attributeForName:@"x"] stringValue];
NSLog(@"time = %@,x = %@",time,x);
}
</pre>