iOS - kissXML的使用【OC】

使用cocoapods工具导入工程

 pod 'KissXML', '~> 5.1.2'

利用kissXML创建一个XML文件(如地图的GPX信息文件)


// 1.判断加速计是否可用
if (!self.motionManager.isAccelerometerAvailable) {
myLog(@"加速计不可用");
return;
}
// 2.设置采样间隔
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创建文件成功");
    }else{
        [FilePathClass filePathDeleteFileName:@"location.gpx"];
        myLog(@"gpx文件删除成功");
        if ([[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]) {
            myLog(@"gpx文件创建成功");
        }
    }
}

//+++++++++++++++++++++++创建XML元素节点++++++++++++++++++++++++++++++
//创建gpx文件后
//XML头字符串
__block NSString *xmlString = @"\n";
//gpx根节点
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) { // 当采样到加速计信息时就会执行
    if (error) return;

    // 4.获取加速计信息
    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子节点
    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头追加节点字符串+++++++++++++++++++++++
xmlString = [xmlString stringByAppendingString:[gpxElement XMLString]];
//+++++++++++++++++++++++写到本地文件+++++++++++++++++++++++
[xmlString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

//+++++++++++++++++++++++读取本地文件中的内容+++++++++++++++++++++++
NSString *str = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:filePath] encoding:kCFStringEncodingUTF8 error:nil];
myLog(@"xmlStr:%@",str);

读取解析XML文件


//获取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:@"//wpt" error:nil];

//遍历每个元素
for (DDXMLElement *obj in children) {

     NSString *time =[[obj attributeForName:@"time"] stringValue];
     NSString *x =[[obj attributeForName:@"x"] stringValue];

     NSLog(@"time = %@,x = %@",time,x);
}

你可能感兴趣的:(iOS - kissXML的使用【OC】)